DSA:Binary Search
class binarysearch
{
int a[];
int i,j,n=0,mid,low=0,high,item;
public binarysearch(int max)
{
a=new int[max];
n=0;
}
public void insert(int item)
{
a[n]=item;
n++;
}
public void display()
{
for(i=0;i
System.out.print(a[i]+"\t");
}
public void binsearch(int key)
{
int flag=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
flag=1;
break;
}
else if(a[mid]
low=mid+1;
else
high=mid-1;
}
if(flag==1)
{
System.out.print("the element is found");
System.out.print("the element is"+(mid+1)+"th position");
}
else
System.out.print("the element is not found");
}
}
No comments:
Post a Comment