Binary Search in C
//Binary Search
#include<stdio.h>
void main()
{
int i,n,a[10],mid,low,high,key, found=0,position;
printf("\n Enter the number of elements:\n");
scanf("%d",&n);
printf("Enter the array element in the ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the key element to be searched\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
found=1;
position=mid+1;
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(found ==1)
printf("Item found in position : %d",position);
else
printf("\n Item not found\n");
}
OUTPUT
LOGIC
Step 1:
0 Comments