C Program for Binary Search using Recursion
Write a Program for Binary Search using Recursion
// C Program for Binary Search using Recursion
#include <stdio.h>
int binarySearch(int A[], int start, int end, int key)
{
while(start <= end)
{
int mid = (start + end)/2;
if(A[mid] == key)
{
return mid;
}
else if(A[mid] > key)
{
return binarySearch(A, start, mid - 1, key);
}
else
{
return binarySearch(A, mid + 1, end, key);
}
}
return -1;
}
int main()
{
int A[] = { 11, 22, 33, 44, 55, 66, 77};
int n = sizeof(A) / sizeof(A[0]); //n is the size of the array
int key;
printf("Array Elements :--> ");
for(int i=0; i<n; i++)
printf("%d ", A[i]);
printf("\nEnter Key Element to Search :--> ");
scanf("%d", &key);
int result = binarySearch(A, 0, n - 1, key);
if(result == -1)
{
printf("\nElement is not present in array");
}
else
{
printf("\nElement is present at index %d", result);
}
return 0;
}
Output:
Array Elements :--> 11 22 33 44 55 66 77
Enter Key Element to Search :--> 3