C Program to search a key element using Binary Search using Functions
Program
#include<stdio.h>
int binary_search(int a[], int low, int high, int key)
{
int mid, flag;
flag = 0;
while(low <= high)
{
mid = (low + high) / 2;
if(key > a[mid])
{
low = mid + 1;
}
else if(key < a[mid])
{
high = mid - 1;
}
else if(a[mid] == key)
{
flag = 1;
break;
}
}
if(flag == 1)
printf("Key element found at position %d\n", mid + 1);
else
printf("Key element not found\n");
}
void main()
{
int n, i, key, low, high, a[10];
printf("Enter the size of the array\n");
scanf("%d", &n);
printf("Enter %d numbers in ascending order\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the key element to be searched\n");
scanf("%d", &key);
binary_search(a, 0, n - 1, key);
}
Output 1
Enter the size of the array
5
Enter 5 numbers in ascending order
50
60
77
80
99
Enter the key element to be searched
12
Key element not found
Output 2
Enter the size of the array
6
Enter 6 numbers in ascending order
12
15
17
18
20
22
Enter the key element to be searched
22
Key element found at position 6
Output 3
Enter the size of the array
5
Enter 5 numbers in ascending order
10
20
30
40
50
Enter the key element to be searched
10
Key element found at position 1