#include<iostream.h>
#include<conio.h>
int BinarySearch(int a[],int n,int d)
{
int b=0,e=n-1,m;
while(b<=e)
{
m=(b+e)/2;
if(a[m]==d)
return m;
else
if(a[m]<d)
b=m+1;
else
e=m-1;
}
return 0;
}
void main()
{
clrscr();
int a[50],i,n,d,flag;
cout<<"Enter the number of elements ";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the element to be searched";
cin>>d;
flag=BinarySearch(a,n,d);
if(flag==0)
cout<<"Element Not Found";
else
cout<<"Element found at location"<<flag+1;
getch();
}
#include<conio.h>
int BinarySearch(int a[],int n,int d)
{
int b=0,e=n-1,m;
while(b<=e)
{
m=(b+e)/2;
if(a[m]==d)
return m;
else
if(a[m]<d)
b=m+1;
else
e=m-1;
}
return 0;
}
void main()
{
clrscr();
int a[50],i,n,d,flag;
cout<<"Enter the number of elements ";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the element to be searched";
cin>>d;
flag=BinarySearch(a,n,d);
if(flag==0)
cout<<"Element Not Found";
else
cout<<"Element found at location"<<flag+1;
getch();
}