Binary Search
Binary search picks a midpoint and uses sorted order or a monotone yes/no test to discard a whole interval of impossible answers.
Picture the idea
A numbered shelf tells you which direction to walk after opening one box.
When to use it
Check that the candidates are ordered or that feasibility changes from false to true only once.
How it works
In [3, 7, 12, 19, 25], searching for 19 starts at 12. The first three values are too small, leaving [19, 25].
- Choose a midpoint inside the current bounds.
- Read the midpoint.
- Keep the only interval that could still contain the answer.
Searching n sorted items takes O(log n) comparisons and O(1) auxiliary memory iteratively.
Watch for this mistake
In a closed interval, moving the lower bound to mid on a failed probe can keep the same interval forever.
Use a two-element input to demonstrate strict progress of both branches.