/*PROGRAM TO IMPLEMENT BINARY SEARCH*/
#include<conio.h>
#include<iostream.h>
class binry_srch
{
int n,low,high,mid,srch,i,a[10];
public:
void input();
void result();
};
void binry_srch::input()
{
cout<<“Enter the size of array: “;
cin>>n;
cout<<“Enter “<<n<<” elements in the array:\n”;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<“Elements in the array are: “<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<“ “;
}
cout<<“\nEnter the element you want to search: “;
cin>>srch;
}
void binry_srch::result()
{
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(srch<a[mid])
high=mid-1;
else if(srch>a[mid])
low=mid+1;
else
{
cout<<“Element is present at “<<mid+1<<”
location”;
break;
}
}
if(low>high)
cout<<“Element is not found”;
}
void main()
{
binry_srch b1;
clrscr();
b1.input();
b1.result();
getch();
}