CalcSnippets Search
Algorithms 3 min read

Binary Search Explained with Practical Examples

Learn binary search in plain English, including sorted data, loop boundaries, common bugs, and real-world uses beyond coding interviews.

Binary search works by discarding impossible answers

Binary search is an algorithm for finding a value, boundary, or answer by repeatedly cutting the search space in half. It works when the data is sorted or when the condition being tested changes in one direction. Instead of checking every item, you inspect the middle and decide which half cannot contain the answer.

This is why binary search is fast. Searching one million sorted items takes about twenty comparisons, not one million. The same idea appears in libraries, databases, file formats, ranking systems, and many optimization problems.

It is not only for exact matches

Beginners often learn binary search as "find this number in a sorted array." That is useful, but the broader pattern is more powerful. You can find the first item greater than or equal to a value, the last valid position, the minimum capacity that satisfies a requirement, or the earliest time when a condition becomes true.

  • Use exact search when the target may be present in sorted data.
  • Use lower-bound search when you need the first acceptable position.
  • Use answer-space search when the answer is numeric and validity is monotonic.
  • Use library helpers when available, but understand their boundary rules.

Most bugs are boundary bugs

Binary search failures usually come from mixing inclusive and exclusive ranges, updating the wrong boundary, or writing a loop that never shrinks. Pick one style and stick to it. For example, use left < right for lower-bound searches and make sure every branch moves either left or right.

When debugging, write down what the variables mean. Does right point to the last possible answer, or one position past it? Can mid equal left forever? Which branch removes the middle value from consideration? These questions expose most mistakes quickly.

Practice the decision, not just the code

The mental model matters more than a memorized snippet. Ask: what part of the range can no longer contain the answer? If that sentence is not clear, the code will probably be fragile. Once the decision is clear, implementation becomes much easier to trust.

Binary search is a small algorithm with a large reach. It teaches developers to use structure in the data, prove why an answer cannot be in part of the range, and turn slow search into precise elimination.

Use tests to lock down boundaries

Binary search code deserves tests around empty arrays, one item, two items, missing targets, duplicate values, first position, last position, and values outside the range. These cases are where off-by-one errors hide. If the function searches an answer space, test the smallest valid answer and the first invalid one.

When a binary search fails, printing left, right, and mid for a tiny example often reveals the bug. The algorithm is fast because it is strict; debugging should be just as precise.

Keep reading

Related guides