VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-for-binary-search-recursive-and-iterative/

⇱ C Program for Binary Search - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program for Binary Search

Last Updated : 12 Dec, 2025

Binary Search is an interval searching algorithm that searches for an item in the sorted list. It works by repeatedly dividing the list into two equal parts and then searching for the item in the part where it can possibly exist.

Rules to Apply Binary Search

Unlike linear search, there are a few conditions for applying binary search:

  • The list must be sorted.
  • Random access to the list members.

It means that we cannot use the binary search with unsorted or linked data structures.

Algorithm

  • Let key be the element we are searching for, and the array be sorted in the ascending order.Compare key with the middle element of the array.
  • If key matches with the middle element, we return the index of the middle element.
  • Else if key is greater than the middle element, it means that key can only lie in the right half subarray after the middle element. So we repeat steps 1 to 4 for the right half subarray and leave the left half subarray.
  • Else if key is smaller than the middle element, it means that target can only lie in the left half subarray before the middle element. So, we repeat steps 1 to 4 for the left half subarray.
  • We will keep doing that till we find key or there is no element left in the subarray being considered.

As we can see, the binary search ignores the half elements of the subarray after each pass. Due to this, it is able to reduce the time required for searching the element in the array as compared to linear search.

Working

Lets understand the working of above algorithm using an example. Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91} and the key = 23

Implementation

1. Iterative Implementation of Binary Search in C

  • Create a function that takes an array, left index, right index, and the key to be searched.
  • Use a loop to iterate while the subarray has elements, i.e., left <= right.
  • Calculate the midpoint mid.
  • Compare the key with the middle element arr[mid].
  • If the key matches the middle element, return mid.
  • If the key is greater than the middle element, adjust the left index to search the right subarray by setting left = mid + 1.
  • If the key is smaller than the middle element, adjust the right index to search the left subarray by setting right = mid - 1.
  • If the key is not found after the loop ends, return -1.

The following C program implements the binary search algorithm using iteration:


Output
Element is present at index 5

Time Complexity: O(log N), where N is the number of elements in the array.
Auxiliary Space: O(1)

2. Recursive Implementation of Binary Search in C

  • Create a function that takes an array, left index, right index, and the key to be searched.
  • Check if the subarray has elements to search i.e. left < right. If not, return -1.
  • Calculate the midpoint mid.
  • Compare the key with arr[mid].
  • If the key matches the middle element, return mid.
  • If the key is smaller, call the function again for the left subarray by passing right = mid - 1.
  • If the key is larger, call the function again for the right subarray by passing left = mid + 1.

The following C program implements the binary search algorithm using recursion:


Output
Element is present at index 5

Time Complexity: O(log N)
Auxiliary Space: O(log N), where N is the number of elements in the array.

Related Articles:

Comment
Article Tags: