Beginner · Interactive lesson

Binary search

One comparison. Half the possibilities. Explore how a sorted array makes searching faster.

The interactive player is loading. If it does not appear, you can still read the explanation below.

THE IDEA BEHIND THE STEPS

How it works

Binary search works on a sorted array. Compare the middle value with the target, then keep only the half that can contain it. Repeat until you find a match or the candidate range is empty.

Watch the low, mid, and high pointers. The dimmed cells have been ruled out. Each comparison cuts the search space roughly in half: O(log n) time and O(1) extra algorithm space. The visualization stores steps for replay; that is separate from the algorithm’s space complexity.

Duplicates are allowed. This version returns any matching index, not necessarily the first. An empty array has no match.