Learn CS Visual

How Binary Search Works

Binary search works on an array that's already sorted. It compares the middle value of the search range with the target, then narrows to the second half if the target is larger or the first half if it's smaller. Each round of narrowing cuts the search range in half, so it finds the target in far fewer comparisons than linear search.

Searching for target 4 in [1, 3, 4, 6, 7, 8, 9, 12], what's the first middle value compared?