DSA · Reimagined
Searching easy time O(log n) space O(1)

Binary Search

On a sorted array, compare the key with the middle element and throw away the half it cannot be in. Each step halves what’s left, so it finishes in log n comparisons.

2
0
5
1
8
2
12
3
16
4
23
5
38
6
56
7
72
8
91
9

Search a sorted array for 23. The whole array is in range.

1 / 6

Practice

Machine twin: /dsa-viz-v2/binary-search.json — the full deterministic run as structured data.