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).
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.
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.
mid - 1.mid + 1.#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;
}
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!
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.
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.
The time complexity is O(log n), making it vastly superior to linear search for large datasets.
What is the Full Form of MS DOS?
Learn the full form of MS DOS. Discover the history of the Microsoft Disk Operating System and how it paved the way for modern Windows.
Multiprogramming Operating System
Learn what a multiprogramming operating system is. Understand how it keeps the CPU busy by switching between multiple programs and its advantages.
Palindrome Program in Java
Learn how to write a palindrome program in Java. Includes programs to check if a string and a number is a palindrome with output.
Difference Between Primary and Secondary Memory
Learn the key differences between Primary Memory (RAM/ROM) and Secondary Memory (Hard Drives/SSD). Understand volatility, speed, and CPU access.
What is the Print Preview Shortcut Key?
Learn the keyboard shortcut key for Print Preview in Windows, Microsoft Word, Excel, and Web browsers like Chrome.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.