C Program to Perform Binary Search using Recursion

This is a C Program to search an element in an Array using Binary Search Algorithm using recursion.

Problem Description

We have to create a C Program which inputs a sorted array and tells whether the key searched is present in array or not using Binary Search Algorithm recursively. We have to take array and the key as an input from the user.

Expected Input and Output

1. Average Case: When the element to be searched is other than the middle element.
For example:

If the input array is {1, 2, 3, 4, 5, 6}
and the key to be searched for is 6
then the expected output will be "Search Successful".

Average case time complexity: O(log n).

2. Best Case: If the element which is searched is the middle element of the array.
For example:

advertisement
advertisement
If the input array is {1, 5, 9}
and the key to be searched is 5,
then the expected output will be "Search Successful".

Best case time complexity: O(1).

3. Worst Case: If the element to be searched for is not present in the array.
For example:

Note: Join free Sanfoundry classes at Telegram or Youtube
If the input array is {1, 3, 6, 8, 9}
and the key to be searched for is 4,
then the expected output will be "Search Unsuccessful".

Worst case time complexity: O(log n).

Problem Solution

1. We will be having an array of numbers, we just need to find out the whether the element is present in an array or not.
2. It can be done using Binary Search by recursion or iteration methods. Here in this problem we will do it using recursion.
3. The basic idea behind Binary Search is that the array in which it is applied upon should be sorted. It divides the whole array into two halves and proceeds to look for the key in suitable part of divided array.

Program/Source Code

Here is source code of the C Program to perform Binary Search using recursion. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

advertisement
  1. /*
  2.  * C Program to Perform Binary Search using Recursion
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. void binary_search(int [], int, int, int);
  8. void bubble_sort(int [], int);
  9.  
  10. int main()
  11. {
  12.     int key, size, i;
  13.     int list[25];
  14.  
  15.     printf("Enter size of a list: ");
  16.     scanf("%d", &size);
  17.     printf("Enter elements\n");
  18.     for(i = 0; i < size; i++)
  19.     {
  20.         scanf("%d",&list[i]);
  21.     }
  22.     bubble_sort(list, size);
  23.     printf("\n");
  24.     printf("Enter key to search\n");
  25.     scanf("%d", &key);
  26.     binary_search(list, 0, size, key);
  27.  
  28. }
  29.  
  30. void bubble_sort(int list[], int size)
  31. {
  32.     int temp, i, j;
  33.     for (i = 0; i < size; i++)
  34.     {
  35.         for (j = i; j < size; j++)
  36.         {
  37.             if (list[i] > list[j])
  38.             {
  39.                 temp = list[i];
  40.                 list[i] = list[j];
  41.                 list[j] = temp;
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. void binary_search(int list[], int lo, int hi, int key)
  48. {
  49.     int mid;
  50.  
  51.     if (lo > hi)
  52.     {
  53.         printf("Key not found\n");
  54.         return;
  55.     }
  56.     mid = (lo + hi) / 2;
  57.     if (list[mid] == key)
  58.     {
  59.         printf("Key found\n");
  60.     }
  61.     else if (list[mid] > key)
  62.     {
  63.         binary_search(list, lo, mid - 1, key);
  64.     }
  65.     else if (list[mid] < key)
  66.     {
  67.         binary_search(list, mid + 1, hi, key);
  68.     }
  69. }
Program Explanation

1. A Binary Search is a quick and efficient method of finding a specific target value from a set of ordered items. A Binary Search, also known as a half-interval search.
2. In Binary Search the key value which is to be searched is compared to the middle element of the array. If the key value is less than or greater than this middle element, the algorithm knows which half of the array to continue searching in because the array is sorted.
3. This process is repeated until we discover the element. In each step this algorithm divides the array size by half and the Binary search will be successful if it is able to locate the element in array, but if it cannot find the element in array it simply returns -1 or prints “Key not Found”.
4. Worst case time complexity of Binary Search it is O(log n). The Best case time complexity of Binary Search is O(1). The only condition for implementing Binary Search is that the array should be sorted.

Runtime Test Cases
1. Enter size of a list: 6
   Enter elements
   1
   2
   3
   4
   5
   6
 
   Enter key to search
   6
   Key found
 
2. Enter size of a list: 3
   Enter elements
   1
   5
   9
 
   Enter key to search
   5
   Key found
 
3. Enter size of a list: 5
   Enter elements
   1
   3
   6
   8
   9
 
   Enter key to search
   4
   Key not found

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Searching and Sorting, go to C Programming Examples on Searching and Sorting. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.