Write a Binary Search program

Answer Posted / jamshed

SHORTEST BINARY SEARCH PROGRAM


import java.io.*;
class Binary
{
public static void main()throws IOException
{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int a[]={5,7,9,11,15,20,30,45,89,97};
int n,c=0,l=0,u=9,m;

System.out.println("Enter a Number to Search");
n=Integer.parseInt(br.readLine());
while(u>l)
{
m=(l+u)/2;
if(a[m]==n)
{
c++;
break;
}
else if(a[m]>n)
u=m;
else if(a[m]<n)
l=m+1;
}
if(c==0)
System.out.println("Not Found");
else
System.out.println("Found");
}
}

Is This Answer Correct ?    14 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between arraylist and list?

556


What do you mean by probing?

589


What is data type and its types?

615


Which of the collections allows null as the key?

578


How is the front of the queue calculated in data structure?

514






What are the 3 types of measurement?

638


Is an arraylist an object?

562


What is a node in it?

624


What is priority queue in data structure?

591


State the advantages of using postfix notations?

655


How would you sort words in a large file?

639


Advanced problems related to Data Structures were asked

620


Why do we use stacks?

567


Define forest?

669


Which is faster quick sort or merge sort?

561