#include<stdio.h>
#include<conio.h>
void main()
{
int beg,
mid, end,a[20],item,loc,i,n;
clrscr();
printf(“Enter
the size\n”);
scanf(%d”,&n);
printf(“Enter
the elements\n”);
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
beg=1;
end=n;
mid=(beg+end)/2;
printf(“Enter
the item to be searched\n”);
scanf(“%d”,&item);
while((beg<=end)&&(a[mid]!=item))
{
if(item<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
printf(“The
item is at mid = %d”,mid);
if(beg>end)
{
loc=0;
}
else
{
loc=mid;
}
printf(“The
location of the item\n”);
printf(“%d”,
loc);
getch();
}
No comments:
Post a Comment