Skip to content

Binary Search

Task: Find 23 in this sorted array

TARGET23mid4111723314256687901234567823 found at index 31VALUE LEFT9 values. 4 comparisons.

What you are watching

Binary search over the sorted array 4, 11, 17, 23, 31, 42, 56, 68, 79, looking for 23. Each step checks the middle value of the range still in play and discards the half that cannot contain the target. The range shrinks from 9 values to 9, then 4, then 2, then 1, and 23 is found at index 3 after 4 comparisons. Because every step halves what is left, the work grows with the logarithm of the input rather than with the input itself.