Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 are sorting algorithms used for?

925


What is merge sort in daa?

821


What is array and string?

886


Explain binary searching, Fibonacci search.

897


What is data type in data structure?

967


Which sorting is best for large data?

866


What is a data structure node class?

965


Will this code give error if I try to add two heterogeneous elements in the arraylist? And why?

894


Who created quicksort?

925


Explain what is the bucket size, when the overlapping and collision occur at same time?

967


What are the disadvantages of sequential storage?

1179


where do you use double linked list?

997


What is stack and its operations?

795


Is selection sort greedy?

828


How do you sort a collection in descending order?

866