ReviseAlgo Logo
EasyVery High Frequency25 min

Binary Search Fundamentals

Left/right boundary templates — the correct way to write bug-free binary search.

Binary search halves the search space each step. Requires sorted data or monotonic condition. Three templates: exact match, left-bound, right-bound. O(log n).

Binary Search

Operations — Step by Step

Binary Search (Exact Match)

O(log n)Halves search space each step. 1 million elements → at most 20 steps
Step-by-step1 / 4
lo
1
0
marker
3
1
marker
5
2
mid
7
3
marker
9
4
marker
11
5
hi
13
6

lo=0, hi=6, target=5

Full array. mid = 0+(6-0)/2 = 3. arr[3]=7.