Binary Search (Recursive and Iterative) in C Program


Binary Search is a search algorithm that is used to find the position of an element (target value ) in a sorted array. The array should be sorted prior to applying a binary search.

Binary search is also known by these names, logarithmic search, binary chop, half interval search.

Working

The binary search algorithm works by comparing the element to be searched by the middle element of the array and based on this comparison follows the required procedure.

Case 1element = middle, the element is found return the index.

Case 2element > middle, search for the element in the sub-array starting from middle+1 index to n.

Case 3element < middle, search for element in the sub-array starting from 0 index to middle -1.

ALGORITHM

Parameters inital_value , end_value

Step 1 : Find the middle element of array. using ,
middle = initial_value + end_value / 2 ;
Step 2 : If middle = element, return ‘element found’ and index.
Step 3 : if middle > element, call the function with end_value = middle - 1 .
Step 4 : if middle < element, call the function with start_value = middle + 1 .
Step 5 : exit.

The implementation of the binary search algorithm function uses the call to function again and again. This call can be of two types −

  • Iterative
  • Recursive

Iterative call is looping over the same block of code multiple times ]

Recursive call is calling the same function again and again.

PROGRAM TO IMPLEMENT BINARY SEARCH USING ITERATIVE CALL

Example

 Live Demo

#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
   while (start_index <= end_index){
      int middle = start_index + (end_index- start_index )/2;
      if (array[middle] == element)
         return middle;
      if (array[middle] < element)
         start_index = middle + 1;
      else
         end_index = middle - 1;
   }
   return -1;
}
int main(void){
   int array[] = {1, 4, 7, 9, 16, 56, 70};
   int n = 7;
   int element = 16;
   int found_index = iterativeBinarySearch(array, 0, n-1, element);
   if(found_index == -1 ) {
      printf("Element not found in the array ");
   }
   else {
      printf("Element found at index : %d",found_index);
   }
   return 0;
}

Output

Element found at index : 4

PROGRAM TO IMPLEMENT BINARY SEARCH USING RECURSIVE CALL

Example

 Live Demo

#include <stdio.h>
int recursiveBinarySearch(int array[], int start_index, int end_index, int element){
   if (end_index >= start_index){
      int middle = start_index + (end_index - start_index )/2;
      if (array[middle] == element)
         return middle;
      if (array[middle] > element)
         return recursiveBinarySearch(array, start_index, middle-1, element);
      return recursiveBinarySearch(array, middle+1, end_index, element);
   }
   return -1;
}
int main(void){
   int array[] = {1, 4, 7, 9, 16, 56, 70};
   int n = 7;
   int element = 9;
   int found_index = recursiveBinarySearch(array, 0, n-1, element);
   if(found_index == -1 ) {
      printf("Element not found in the array ");
   }
   else {
      printf("Element found at index : %d",found_index);
   }
   return 0;
}

Output

Element found at index : 3

Updated on: 02-Sep-2023

66K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements