Binary Search Explorer

Watch binary search on a sorted row of numbered boxes — each probe checks the middle, compares to the target, and greys out the discarded half, with plain-English commentary narrating every step.

This interactive binary search visualization turns the classic "halving" algorithm into something you can watch happen. Binary search finds a target value in a sorted list by repeatedly looking at the middle element and throwing away the half that cannot contain the key. The animation draws the list as a row of numbered boxes with l, mid, and r pointers, so every probe becomes a single question: is the middle value smaller, bigger, or equal to the target you are looking for?

Press play, or step through one probe at a time. On each probe the midpoint m = ⌊(l + r)/2⌋ is highlighted in orange, its value is compared with the target, and the losing half greys out as the live window [l, r] collapses inward. A running commentary narrates each step in plain English, such as "middle = index 11 (value 58); 58 is greater than 55, so go left", and a growing log records how many items are still in play. Because each probe halves the search space, the count falls fast and the search always finishes within the ⌈log₂ n⌉ bound. That is the whole reason binary search is logarithmic: doubling the list size adds just one more probe.

Use the presets to explore every case. Hit (middle) finds the key on the very first probe, the best case. First element and Last element force the window to crawl all the way to one edge, the worst case, using exactly ⌈log₂ n⌉ probes. Miss (between) and Absent target show how the search terminates when the value is not present: the window shrinks until l > r, an empty interval, which is precisely how the algorithm reports "not found." You can also drag the target slider to any value, toggle whether it is present, and resize the list from 7 up to 63 items to confirm the probe count barely grows. Watching the interval halve makes the off-by-one boundary update (l = m + 1, r = m - 1, never m) and the empty-interval stop condition concrete instead of abstract.