//BINARY SEARCH
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50];
clrscr();
cout<<"enter no of elements:";
int n;
cin>>n;
cout<<"enter in asceding order::";
for(int i=0;i<n;i++)
{
cout<<"a["<<i<<"]:";
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]:"<<a[i]<<endl;
}
cout<<"enter no which you want to saerch:";
int m;
cin>>m;
int fi=0;
int la=n-1;
for(;!(m>a[la]||m<a[fi]);)
{
int mid=(fi+la)/2;
if(m==a[mid])
{
cout<<"your no at:"<<mid;
break;
}
else if(m>a[mid])
{
fi=mid+1;
}
else
{
la=mid-1;
}
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50];
clrscr();
cout<<"enter no of elements:";
int n;
cin>>n;
cout<<"enter in asceding order::";
for(int i=0;i<n;i++)
{
cout<<"a["<<i<<"]:";
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]:"<<a[i]<<endl;
}
cout<<"enter no which you want to saerch:";
int m;
cin>>m;
int fi=0;
int la=n-1;
for(;!(m>a[la]||m<a[fi]);)
{
int mid=(fi+la)/2;
if(m==a[mid])
{
cout<<"your no at:"<<mid;
break;
}
else if(m>a[mid])
{
fi=mid+1;
}
else
{
la=mid-1;
}
}
getch();
}