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.
Hard Copy and Soft Copy โ Difference and Examples
Learn the difference between hard copy and soft copy. Hard copy is physical printed output. Soft copy is digital output viewed on screen. Examples and comparisons.
High Level Language and Hybrid Computer
Learn the definitions of High Level Language (like Python, Java) and Hybrid Computers. Basic concepts for computer science students.
How Many Rows and Columns are there in MS Excel?
Find out the exact maximum number of rows and columns in Microsoft Excel. Learn about the 1 million row limit and how the columns are named from A to XFD.
What is the Full Form of IIS in Computers?
Learn the full form of IIS in Computer Science. Understand how Microsoft's Internet Information Services works to host massive websites and web applications.
Impact Printer and Non-Impact Printer
Learn the difference between impact printers (like dot matrix) and non-impact printers (like inkjet and laser). Types, examples, and key differences explained.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.