#include<stdio.h>
#include<conio.h>
int binsearch(int a[],int lb,int ub,int target)
{
int i,mid;
while(lb<=ub)
{
mid=(lb+ub)/2;
if(target==a[mid])
return mid;
else
if(target<a[mid])
{
ub=mid-1;
i=binsearch(a,lb,ub,target);
return i;
}
else
{
lb=mid+1;
i=binsearch(a,lb,ub,target);
return i;
}
}
return (-1);
}
void main()
{
int a[10],n,target,i;
clrscr();
printf("\n Enter the size of
the array ");
scanf("%d",&n);
printf("\n Enter the array
elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element
to find ");
scanf("%d",&target);
printf("\n The element %d is
found in %d position",target,binsearch(a,0,n-1,target));
getch();
}
No comments:
Post a Comment