Back

Binary Search

2025-01-12

Written by: Juri Breslauer

Algorithm Notes

Binary search cuts the search space in half

Binary search works only on sorted data. At each step it checks the middle value and removes the half that cannot contain the target.

Interactive demo

Binary search demo

Ready
(0 + 7) / 2 = 3
Low 0
Mid -
High 7
Steps 0

How It Works

Binary search keeps two borders: low and high. The target can only be inside that range.

Complexity

Binary search has O(log₂n) time complexity because every step removes half of the remaining values. A list of one million sorted items takes about 20 checks, not one million.

Here are examples of the same binary search algorithm in different programming languages.