Study Guides/Computer Science/Binary Search in C
Study Guide ยท Computer Science

Binary Search Algorithm in C Programming

In computer science, searching for a specific number in a massive database can take a long time. Binary Search is an incredibly fast and efficient search algorithm. However, it has one strict rule: It only works on an array that is already sorted (arranged in ascending or descending order).

Question (Click to Flip)

What is binary search in C?

Answer

Binary search is an efficient search algorithm in C that finds the position of a target value within a sorted array by repeatedly dividing the search interval in half.

Card 1 of 3 free previews

Key Facts

Pre-requisite: The array MUST be sorted.

Strategy: Divide and Conquer.

Time Complexity: O(log n) - Extremely fast for large datasets.

Space Complexity: O(1) for iterative approach.

How Binary Search Works (The Logic)

Binary search uses a 'divide and conquer' strategy. Instead of checking every single element one by one (Linear Search), it repeatedly divides the array in half.

Imagine searching for the word 'Mango' in a dictionary. You don't read from page 1. You open the book to the middle. If you land on 'Orange', you know 'Mango' must be in the left half, so you completely ignore the right half. You open the left half to the middle, and repeat until you find it.

The Algorithm Steps

  1. Find the middle element of the sorted array.
  2. Compare the search key with the middle element.
  3. If it matches, return the index.
  4. If the search key is smaller than the middle element, it must lie in the left half. Update the 'high' pointer to mid - 1.
  5. If the search key is larger, it must lie in the right half. Update the 'low' pointer to mid + 1.
  6. Repeat until the item is found or the low pointer exceeds the high pointer (item not found).

C Code Example (Iterative)

#include <stdio.h>

int binarySearch(int arr[], int size, int key) {
    int low = 0, high = size - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        
        if (arr[mid] == key)
            return mid; // Found the key
            
        if (arr[mid] < key)
            low = mid + 1; // Ignore left half
        else
            high = mid - 1; // Ignore right half
    }
    return -1; // Key not found
}

int main() {
    int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
    int size = sizeof(arr) / sizeof(arr[0]);
    int key = 23;
    int result = binarySearch(arr, size, key);
    if(result == -1)
        printf("Element not found");
    else
        printf("Element found at index %d", result);
    return 0;
}

Time Complexity

Because the search space is cut in half at every step, the time complexity of Binary Search is extremely fast: O(log n). To put this in perspective, if you search an array of 1 million elements, a Linear Search could take 1 million steps, but a Binary Search will find the answer in a maximum of just 20 steps!

Questions and Answers

What is binary search in C?+

Binary search is an efficient search algorithm in C that finds the position of a target value within a sorted array by repeatedly dividing the search interval in half.

What is the condition required for binary search?+

The fundamental condition for a binary search to work is that the elements in the array must be pre-sorted in ascending or descending order.

What is the time complexity of a binary search?+

The time complexity is O(log n), making it vastly superior to linear search for large datasets.

More in Computer Science

Study Smarter with Shinyu.ai

Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ€” free, no signup required.