Computer ScienceAlgorithmsEasy

Binary Search

Also known as:Half-interval searchLogarithmic searchBinary chop

Binary search is a highly efficient search algorithm that finds the position of a target value within a sorted array by repeatedly dividing the search interval in half. At each step, it compares the target to the middle element and discards the half in which the target cannot lie. With a time complexity of O(log n), binary search is dramatically faster than linear search for large sorted datasets.

Key Formula

mid = floor((low + high) / 2)

LaTeX: mid = \left\lfloor \frac{low + high}{2} \right\rfloor

SymbolMeaningUnit
midIndex of the middle elementindex
lowLower bound index of the current search rangeindex
highUpper bound index of the current search rangeindex

Worked Example

Problem

Search for the value 37 in the sorted array [2, 5, 8, 12, 16, 23, 37, 45, 56, 72].

Solution

Step 1: low=0, high=9, mid=4 → array[4]=16. 37 > 16, so search right half. Step 2: low=5, high=9, mid=7 → array[7]=45. 37 < 45, so search left half. Step 3: low=5, high=6, mid=5 → array[5]=23. 37 > 23, so search right half. Step 4: low=6, high=6, mid=6 → array[6]=37. Target found!

Answer

Value 37 found at index 6 in 4 steps (vs. 7 steps for linear search).

Binary Search vs Linear Search Comparison

PropertyBinary SearchLinear Search
Time ComplexityO(log n)O(n)
Space ComplexityO(1) iterativeO(1)
PrerequisiteArray must be sortedNo requirement
Steps for n=1,000,000~20~500,000 (avg)
Best CaseO(1)O(1)
Worst CaseO(log n)O(n)

Interactive Tools

VisuAlgo

Animated binary search visualisation

Open Tool

Khan Academy – Binary Search

Step-by-step binary search lessons

Open Tool

Brilliant.org

Binary search problems and theory

Open Tool
Diagram illustrating the binary search process on a sorted array

Wikimedia Commons, CC BY-SA

Related Terms

The concept of binary search derives from the mathematical principle of bisection, known since antiquity. The first formal description of binary search for sorted lists was given by John Mauchly in 1946. The algorithm was first published by D.H. Lehmer in 1960.

binary-searchsearchingsorted-arraylogarithmicalgorithms