#include<stdio.h>
#include<conio.h>
void main()
{
int n,last,first,mid,key,arr[100];
clrscr();
printf("Enter the number you want to enter:\n");
scanf("%d",&n);
printf("Enter the number in assending order:");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter a number want to serch:");
scanf("%d",&key);
last=n-1;
first=0;
mid=(first+last)/2;
while((first<=last)&&(arr[mid]!=key))
{
if(key<arr[mid])
last=mid-1;
else
first=mid+1;
mid=first+last/2;
}
if(arr[mid]==key)
{
printf("item found at location %d",mid+1);
}
else
{
printf("item doesn't exist");
}
getch();
}
Comments
Post a Comment