Create Binary search

This commit is contained in:
neha510 2023-10-01 18:34:47 +05:30 committed by GitHub
parent e5dad3fa8d
commit fd9236245c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

29
searching/Binary search Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}