Learn

Binary Search: Why Sorted Data Changes the Search

Trace binary search, understand its sorted-data requirement, and account for the cost of maintaining that order.

Use order to discard possibilities

Binary search finds a position in sorted data by repeatedly narrowing the candidate range. Inspect a middle item. If the target is smaller, the target cannot be to its right under the chosen ordering. If it is larger, discard the left side. Continue until you find the item or the remaining range is empty.

The reason this works is the sorted-data assumption. Without it, a smaller middle item says nothing about the values elsewhere. Binary search is not a faster drop-in replacement for every scan. It relies on information that the data’s organization provides.

Trace a complete lookup

Use the sorted values 3, 8, 14, 19, 25, 31, 42, and 60, and search for 31. One possible convention selects the lower middle item, 19. Since 31 is larger, retain only 25, 31, 42, and 60. Their lower middle is 31, so the lookup succeeds after two inspected values.

Searching for 30 instead would narrow to the gap between 25 and 31. An implementation that returns an insertion position can identify that gap without claiming that 30 already exists. This is a different contract from returning a matching item’s index, and the calling code must know which contract it receives.

Halving produces logarithmic growth

Consider a candidate range containing 1,024 items. Successive halvings reduce the size to 512, 256, 128, and so on until only one remains. Ten halvings reduce that size to one. An actual search may require a final comparison, depending on its exact loop and output contract. This is why logarithmic complexity should not be presented as an exact universal comparison count.

The broader point is that multiplying the data size does not multiply the number of narrowing rounds by the same amount. This growth behavior can be valuable for repeated lookups in a large, stable array. It says nothing by itself about the time required to prepare the array.

Preparation and insertion have costs

Python’s bisect documentation illustrates bisection through insertion-point APIs. It also distinguishes the logarithmic search step from the linear movement needed when inserting into a list. Finding where an item belongs and making space for it are different operations.

Suppose an application receives one new reading every moment and immediately inserts it into the middle of a large array. The lookup position might be cheap to find, while moving subsequent entries dominates the work. For a different workload, collecting a batch and sorting it later might be more appropriate. The right choice depends on update frequency, query frequency, ordering requirements, and available data structures.

Correctness lives at the boundaries

An implementation needs a consistent interval convention: are both endpoints included, or is the upper endpoint excluded? It must reduce the range on every unsuccessful step. Otherwise, it can skip a valid item or loop forever. Empty arrays, one item, the first item, the last item, missing values, and repeated values all deserve explicit tests.

Duplicates require another decision. A basic search might return any matching occurrence. An application asking for the first or last match needs a boundary-finding version. For records sorted by timestamp, use the same timestamp comparison when searching; sorting by one field and searching by another breaks the premise.

Binary search demonstrates how organization can replace repeated inspection. Before adopting it, identify the ordering contract and include the maintenance cost in your analysis. Compare it with hash-table lookup when you need exact-key retrieval, and sorting when preparing a reusable order.