search range
mid check
found
2
0
5
1
8
2
12
3
16
4
23
5
38
6
56
7
Target = 23. Array is sorted. Check the middle element first.
Binary search requires a sorted array. Each check eliminates half the remaining elements.
start
1 / 13
SPD0.5×
click to pause · hover for controls · fullscreen for more

Binary Search

Search by halving the range

Visual by thisgirltech

BeginnerSearching

Binary search finds a target value inside a sorted array by repeatedly halving the search space — making it one of the most elegant and efficient algorithms in computer science.

Instead of scanning every element one by one like linear search, binary search always checks the middle element of the current range. If the target is smaller, it discards the entire right half. If larger, it discards the entire left half. Each single check eliminates half the remaining candidates.

This animation walks you through multiple searches on the same sorted array, so you can see exactly how the low and high pointers move, how the mid element is chosen each iteration, and why the search space collapses so quickly.

Logarithmic speed

For an array of 1,000,000 elements, binary search finds any value in at most 20 steps. Linear search could take 1,000,000.

Sorted data only

Binary search requires the array to be sorted in advance. Without sorted order, comparing to the middle tells you nothing useful about which half to discard.

Two flavours

Binary search can be written iteratively (a while loop with low/high pointers) or recursively (passing the range as arguments). Both produce identical results.

A pattern, not just a trick

"Binary search thinking" — eliminate half the possibilities on every decision — appears in dozens of advanced problems: rotated arrays, finding boundaries, square roots, and more.

TIP

Watch how the low/high pointers close in on the target — the search space halves every single step. Count the iterations — it almost never takes more than 5 or 6 even on a 13-element array.

COLOUR LEGEND

Mid element
Search range
Eliminating
Found
Not found
Eliminated