Binary Search
A sorted array and a target. Watch low, high, and mid pointers close in on the answer in logarithmic time.
The cyan range is still possible. The amber cell is the midpoint being compared. Each comparison removes roughly half of the remaining values, so the search needs only about log₂(24) ≈ 5 steps.
What you are seeing
The array is sorted, so one comparison can eliminate half of the remaining elements.
The midpoint splits the current range into a left half and a right half.
If the middle value is too small, move low up; if it is too large, move high down.
Why it matters
Binary search is one of the highest-leverage algorithms you can learn: databases, version control, and search indexes all use the same halving idea.
Complexity & details
Time: O(log n) comparisons. Space: O(1) iterative.
Frequently asked questions
Why does the array need to be sorted?+
The halving step depends on knowing which side of the midpoint can contain the target. Without order, you cannot discard half safely.
What is an off-by-one error in binary search?+
It usually comes from updating low or high incorrectly, causing an infinite loop or skipping the answer. Use low = mid + 1 and high = mid - 1 with a consistent range convention.