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 a weighted graph?

746


What's difference between stack and queue?

698


Explain what are the major data structures used in the network data model?

758


What are threaded binary trees?

704


What is array and structure?

715


What is a simple graph?

682


What is time and space complexity of bubble sort?

593


What does bubble sort do?

599


Write the recursive c function to count the number of nodes present in a binary tree.

638


Differentiate null and void?

645


Can hashmap have same key?

635


Define a relation?

707


In an avl tree, at explain what condition the balancing is to be done?

715


What does a bubble chart show?

624


By Which algorithm, the 8 queens problem is solved?

665