Program of Binary search as same linear search but differ.
Binary search is also a searching technique or method in which
we divide (beginning element + end element) by 2 give middle and
check the lie between element.
#include<stdio.h>
int main()
{
int list[10]={1,2,3,4,5,6,7,8,9,10};//sorted array
int search,beg,end,mid;
printf("Enter the element you can search in the list:\n");
scanf("%d",&search);
beg=0;
end=9;
while(beg<=end)
{
mid = (beg+end)/2;
if(search==list[mid])
{
printf("search successful!\n");
break;
}
else
{
if(search>list[mid])
{
beg=mid+1;
}
else
{
end=mid-1;
}
}
}
if(beg>end)
{
printf("Invalid search!");
}
return 0;
}
Output
Enter the element you can search in the list:
6
search successful!
****** Thank You! *****
Comments
Post a Comment