int main()
{
int arr[100],i,n,x,flag=0,first,last,mid;
printf("Enter size of array:");
scanf("%d",&n);
printf("\n Enter %d integer value one by one:\n",n);
for(i=0; i<n; ++i)
{
scanf("%d",&arr[i]);
}
printf("\nEnter the element to search:");
scanf("%d",&x);
printf("You Enterd number is=%d",x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid])
{
flag=1;
break;
}
else
{
if(x>arr[mid])
first=first+1;
else
last=last-1;
}
}
if(flag==1)
{
printf("\n***The Number is found at:%d position***",mid+1);
}
else
{
printf("\n ****The Number is not found***");
}
return 0;
}
***Input && Output***
6 Comments
Nice
ReplyDeleteIt is simple program for binary Searching.
ReplyDeleteNice
DeleteNice
ReplyDeleteNice
ReplyDeleteThanku
Delete