Here is the simple c program of binary search….
#include<stdio.h>
int arr[10];
int t,b,m,s;
int bsearch(int item)
{ int flag=0;
t=0;
b=s-1;
while(!flag && t<=b)
{
m=(t+b)/2;
if(arr[m]==item)
{
flag=1;
break;
}
if(arr[m]>item)
b=m-1;
else
t=m+1;
}
return(flag);
}
void main()
{
int i,r,itm;
clrscr();
s=0;
for(i=0;i<=9;i++)
{
arr[i]=i+1;
s++;
}
for(i=0;i<=9;i++)
printf(“\n%d”,arr[i]);
printf(“\nEnter item to search: “);
scanf(“%d”,&itm);
r=bsearch(itm);
if(r==1)
printf(“\nItem found”);
else
printf(“\nItem not found”);
getch();
}
Advertisements