CLASS-XIIB-CS (ARRAY-BINARY SEARCH)
#include<conio.h>
#include<iostream.h>
void main()
{
int a[50];
int j,n,data,found,first,mid,last;
//clrscr();
cout<<"\n BINARY SEARCH \n";
cout<<" ENTER TOTAL NO.OF ELEMENTS ( N<=50 )PRESENT IN THE ARRAY (IN ASCENDING ORDER) : ";
cin>>n;
for(j=0;j<n;j++)
{
cout<<"\n Enter element no. "<<(j+1)<<'\t';
cin>>a[j];
}
cout<<" NOW ENTER A DATA TO BE SEARCHED... \n";
cin>>data;
first=0; last=n-1; found=0;
while(first<=last)
{
mid=(first+last)/2;
if(data==a[mid]) {found=1;break;}
if(data<a[mid]) last=mid-1;
else first=mid+1;
}
if(found) cout<<" ELEMENT FOUND AT POSITION "<<(mid+1);
else cout<<" ELEMENT NOT FOUND";
cout<<endl;
getch();
}