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
Which is the fastest sorting?
Is arraylist better than array?
Differentiate between the singly linked list and doubly linked list.
Why do we need sorting?
What are linked list?
What is int data type?
Is hashset thread safe?
Explain about the different lists available in the collection?
What do you mean by balanced trees?
You are given a singly linked list. How would you find out if it contains a loop or not without using temporary space?
What is lifo?
What are the disadvantages of linked list over array?
Which sorting algorithm is worst?
What is stable sorting?
What are trees in data structures?