SearchingAnimated
Binary Search
Repeatedly halve a sorted list by comparing with the middle.
Time
O(log n)
Extra space
O(1)
Use it for
Sorted data with random access.
How it works
- The list must be sorted. Keep a window [low, high] that could still contain the target.
- Look at the middle element.
- If it equals the target, done. If the target is smaller, discard the right half; if larger, discard the left half.
- Repeat on the smaller window until it is empty.
- Each step halves the window, so it takes about log₂ n steps.
Watch it run
Loading animation…