Binary search in array: - C program:
Write a program in c to define binary search in array.
void main()
{
clrscr();
int a[100] , ch , n;
cout<<"enter the nit for the array : ";
cin>>n;
for (int i=0;i
{
cout<<"enter element "<
cin>>a[i];
}
Cout<< "Binary search:"
bsearch (a,n);
getch();
} //main
void bsearch (int a[100] , int n)
{
int f=0,l=n-1,m,i,ele,pos=-1;
cout<<"enter the element to be searched : ";
cin>>ele;
isort(a,n);
clrscr();
if (a[0]
{
while (f<=l && pos=-1)
{
m=(f+l)/2;
if (ele == a[m])
{
pos=m;
}
if (ele>a[m])
f=m+1;
if (ele
l=m-1;
}
if (pos=-1)
cout<<"elememt not found";
else
cout<<"element found at "<
else
{
while (f<=l && pos=-1)
{
m=(f+l)/2;
if (ele == a[m])
{
pos=m;
}
if (ele>a[m])
l=m+1;
if (ele
f=m-1;
}