Illustrates the program for Binary Search.
Program: Binary Search
/*Header Files*/
#include
#include
/*Functions*/
void binary_search(int array[ ], int value, int size)
{
int found=0;
int low=0, high=size-1, mid;
mid = (high+low)/2;
printf("\n\n Searching for %d\n", value);
while((!found)&&(high>=low))
{
printf("Low %d Mid%d High%d\n", low, mid, high);
if(value==array[mid] )
{
printf("Key value searched out at position %d",mid+1);
found=1;
}
else
{if (value
high = mid-1;
else
low = mid+1;
mid = (high+low)/2;
}
}
if (found==1
printf("Search successful");
else
printf("Key value not found");
}
/*Main Function*/
void main(void)
{
int array[100], i;
/*Inputting Values to Array*/
for(i=0;i<100;i++)
{ printf("Enter the name:");
scanf("%d", array[i]);
}
printf("Result of search %d\n", binary_searchy(array,33,100));
printf("Result of search %d\n", binary_searchy(array, 75,100));
printf("Result of search %d\n", binary_searchy(array,1,100));
}