Searching algorithms, step by step
Binary search is the highest-leverage pattern in interviews: it turns a linear scan into a logarithmic one, and the same halving idea generalises far beyond sorted arrays. The visualizer shows the low, high, and mid pointers converging so the invariant stays visible at every step.
The hard part is never the idea — it is the boundary conditions. Watch how mid is computed and whether the search space shrinks on every iteration; those two details are where most buggy implementations go into infinite loops.
Time and space complexity
| Algorithm | Best | Average | Worst | Space | Requires sorted input |
|---|---|---|---|---|---|
| Linear Search | O(1) | O(n) | O(n) | O(1) | No |
| Binary Search | O(1) | O(log n) | O(log n) | O(1) | Yes |
| Jump Search | O(1) | O(√n) | O(√n) | O(1) | Yes |
| Exponential Search | O(1) | O(log n) | O(log n) | O(1) | Yes |
How to use this visualizer
Choose a search track and a target value.
Step through and watch the low/high window shrink after each comparison.
Confirm the invariant: the target, if present, always lies inside the current window.
Search for a value that is absent to see how the loop terminates when low passes high.
Frequently asked questions
In fixed-width integer languages such as Java, C++, or Go, low + high can overflow when both are near the maximum integer, producing a negative midpoint and an out-of-bounds access. Writing low + (high - low) / 2 computes the same midpoint without ever exceeding the larger operand. JavaScript numbers are doubles so it rarely bites there, but interviewers still look for it.
You cannot search unsorted values directly, but you can binary search any monotonic predicate. If some property is false, false, …, false, true, true, …, true across the search space, binary search finds the boundary. That is the basis of "binary search on the answer" problems like Koko Eating Bananas or minimum capacity to ship packages.
O(log n) time and O(1) space when written iteratively. Each comparison discards half the remaining candidates, so an array of a million elements resolves in about 20 steps. A recursive implementation is also O(log n) time but uses O(log n) stack space.