Java Program for Binary Search :
class bsearch
{
static void search(int[]a,int key)
{
int p=-1,h=a.length-1,l=0,m;
while(p==-1 && h>=l)
{
m=(h+l)/2;
if(a[m]>key)
h=m-1;
if(a[m]<key)
l=m+1;
if(a[m]==key)
p=m;
}
if(p==-1)
System.out.print("Search key '"+key+"' not found");
else
System.out.print("Search key '"+key+"' found at : Position "+(p+1));
}
}
class bsearch
{
static void search(int[]a,int key)
{
int p=-1,h=a.length-1,l=0,m;
while(p==-1 && h>=l)
{
m=(h+l)/2;
if(a[m]>key)
h=m-1;
if(a[m]<key)
l=m+1;
if(a[m]==key)
p=m;
}
if(p==-1)
System.out.print("Search key '"+key+"' not found");
else
System.out.print("Search key '"+key+"' found at : Position "+(p+1));
}
}
HI,
ReplyDeleteI want help in JAVA
if you read this comment
then contact me at amaanrizwan007@gmail.com