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 the logic to reverse the array?

550


What is data algorithm?

618


What is immutablelist?

572


What are different types of sorting techniques?

588


How to check array contains value or not?

649






What is 2 dimensional linked list?

672


Explain Array of pointers?

655


What is map entry?

575


List out the advantages of using a linked list?

569


Is hashmap a collection?

593


Is data structures and algorithms important?

584


List some applications of multilinked structures?

613


Why do we use insertion sort?

556


What does each entry in the link list called?

566


What is data type and its types?

613