Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


AliJak
plz answer
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners


Write a Binary Search program that searches an array of ordered data. Compose an ordered array of 500 integers that contains the following series of numbers and run the search on it:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Advertisement
View Answers

March 15, 2010 at 2:51 PM


Hi Friend,

Try the following code:

import java.util.*;
class BinarySearchExample{
public static void main(String[] args){
int counter=0;
int[] intArray = new int[40];
int searchValue = 0, index;
int f1, f2 = 0, f3 = 1;
for (int i = 0; i < intArray.length; i++) {
f1 = f2;
f2 = f3;
f3 = f1 + f2;
intArray[i]=f3;
System.out.println(intArray[i]);
}
System.out.print("Enter a number to search for: ");
Scanner input=new Scanner(System.in);
searchValue = input.nextInt();
index = binarySearch(intArray, searchValue);
if (index != -1) {
System.out.println("Found at index: " + index);
} else {
System.out.println("Not Found");
}
}
static int binarySearch(int[] search, int find) {
int start, end, midPt;
start = 0;
end = search.length - 1;
while (start <= end) {
midPt = (start + end) / 2;
if (search[midPt] == find) {
return midPt;
} else if (search[midPt] < find) {
start = midPt + 1;
} else {
end = midPt - 1;
}
}
return -1;
}
}

Thanks


Related Tutorials/Questions & Answers:
plz give me answer plz
plz give me answer plz  writw a programme to find rank from an array using doubledimmensionalarray
plz give me answer
plz give me answer   description about string class methods   Java string methods
Advertisements
plz give me answer
plz give me answer   description about string class methods   Java string methods
plz give me answer
plz give me answer   description about string class methods   Java string methods
plz give me answer
plz give me answer  Discuss & Give brief description about string class methods   Java string methods
Plz answer - Java Beginners
Plz answer  Create a Java Bean Class ‚?? Customer according to the below given class diagram. Customer... ------------------------------------------------- plz doreply me....thankz alot  
plz answer - Java Beginners
plz answer   Write a Binary Search program that searches an array of ordered data. Compose an ordered array of 500 integers that contains the following series of numbers and run the search on it: 1, 2, 3, 5, 8, 13, 21, 34, 55
Plz send me answer quckly
Plz send me answer quckly  Respected Sir, myself is pavan shrivastava.i want ask a question that is ( we can't create object of interface then how would possible to create object
plz. answer my ques - Java Beginners
plz. answer my ques  I want to ask a prog. prob which would create a prog that would simply take username and password as input and will display ***** while taking passwords as input.(As it is done at login in websites). Also
how the answer is 13 can anyone explain plz
how the answer is 13 can anyone explain plz  public class that { public static void main(String arr[]) { int i=3; int j=1; while(i<4) { //j=i; i=i+j/2; } System.out.println(i
need the answer vry urgently..plz help me...[plzzzzzzzz
need the answer vry urgently..plz help me...[plzzzzzzzz  the question... code...hpw to write in eclipse platform....plz plz mail me...to panda.pragnya7@gmail.com/panda_pragnya@yahoo.com...plz plz...help....me..it can change my lyfe
answer it soon
answer it soon  write a proggram to accept a number and check whether the sum of prime factors of a number is a prime number
java.please answer this...
java.please answer this...   Q1).How we can write a complete JAVA program that computes the following summation formulas..? ‚??_(i=1)^n‚??„??i^2=„?? (n^(2 ) „??(n+1)„??^2)/4
plz plz plz inform me as soon as possible
plz plz plz inform me as soon as possible  ``by using c programs how to type
plz help
plz help  what is the procedure and code to design an interactive GUI in java using swings
Plz Help
Plz Help  Hello Guys!! Plz. Help I need to write a program to display the astrological sign based on your birthday for example If we enter the date of birth in the following format from console--13/08/1990 it should give you
Answer for SCJP Module-1 Question-13
Answer for SCJP Module-1 Question-13  The Answer for SCJP Module-1 Question-13 is missing , I think the answer is option '1', Correct me if I am wrong
plz plz rply as soon as possible
plz plz rply as soon as possible  by using c programs * * * * * s s s s s * s s o s s s s s s s s s s s s s s s s s s s s s s s
pls answer the Trangle pattern
pls answer the Trangle pattern    a b c d e b c d e c d e d e e Please answer and give the code for above triangle
Urgent Answer Req
Urgent Answer Req  how to view the file content which is at client side from jsp in java
ans plz
)); } } } Thanks Post Answer
Please give me the answer.
"int a=08 or 09" its giving compile time error why   "int a=08 or 09" its giving compile time error why ? can any one give me the answer of this please
please answer me
please answer me  iam using html in select tag.in this one option i selected and that option data will be display in table.And use servlet with html and how to retrieve the data from data base
share your answer to this question
share your answer to this question  i need to know what is the TELERIK CONTROL? what is the map suite? what is dunda chart? Also i have to know what are the websites available which give infor regarding this. Let me know how
pls answer these question
pls answer these question  Write a program that prompts the user to input the length and width of a rectangle. Then calculate and prints the rectangle‚??s area and perimeter.   import java.io.*; class rectangle
Is the answer 35, 35?
Is the answer 35, 35?  int a = 5; int b = 7; int num = 35; while (a < b) { System.out.println(?My age is ? + num); a = a + 1; }   Hello Friend, class Check{ public static void main(String[] args
answer me this program
answer me this program  Java Code to Maintain student record,Name, Age, Marks of 5 subjects for a Student,Display Student age and name,Calculate Average of all marks and display it   Java student record import
answer me this program
answer me this program  Java Code to Maintain student record,Name, Age, Marks of 5 subjects for a Student,Display Student age and name,Calculate Average of all marks and display it   Java student record import
please send me the answer - JDBC
please send me the answer -difference between DriverManager and DataDourse   what is Datasourse? What r the advantages? what is the difference between DriverManager and DataDourse
ArrayList question plz solve it
ArrayList question plz solve it  An arraylist object stores an Emp Object, where there is name,age,address of the employee is stored, And i want to access the data in the basis of age based? Provide answer to the question
Advertisements
 

 

 

DMCA.com