#include<iostream.h>
#include<conio.h>
int Bsearch(int[],int,int);
void main()
{
clrscr();
int AR[50],item,n,index;
cout<<"Enter desired array size (max 50)";
cin>>n;
cout<<"enter array element (must be sorted in Asc order)";
for (int i=0;i<n;i++)
cin>>AR[i];
cout<<"enter the element to be searched for";
cin>>item;
index=Bsearch(AR,n,item);
if(index== -1)
cout<<"sorry!!given element could not be found";
else
cout<<"element found at index:"<<index<<",Position"<<index+1<<endl;

}
int Bsearch(int AR[],int size,int item)
{
int beg,last,mid;
beg=0;   last=size-1;
while (beg<=last)
{mid=(beg+last)/2;
if(item==AR[mid])return mid;
else if (item>AR[mid]) beg=mid+1;
else last=mid-1;
}
return-1;
}
Designed By Blogger Templates | Templatelib & Distributed By Blogspot Templates