Skip to content
SimpleScope

Searching

Linear search vs Binary search

Linear search and Binary search both find a value in an array, in different ways. Linear search: scan every element in order. Binary search: halve the range of a sorted array.

At a glance

PropertyLinear searchBinary search
TechniqueScan every element in orderHalve the range of a sorted array
Best case timeO(1)O(1)
Average case timeO(n)O(log n)
Worst case timeO(n)O(log n)
Extra spaceO(1)O(1)
Needs sorted inputNoYes
Needs to jump to any indexNoYes

When to choose each

Linear search: when to choose it

Choose linear search when you cannot pay for order. It is the only one of the two that works on data as it arrives: unsorted, in a linked list, streaming past you once. Sorting the array first costs more than the single scan you were trying to avoid, so for a one-off lookup linear search wins outright, however large the array. It also wins when the thing you are looking for tends to be near the front — a most-recently-used list, a log read newest-first — because it can stop at the first element, where halving still has to walk down to it.

Binary search: when to choose it

Choose binary search when the array is already sorted, reachable by index, and searched more than once. Its advantage is not a constant factor, it is the shape of the growth: every comparison throws away half of what is left, so the work grows with log n instead of n. Doubling the array adds one comparison. The cost is three preconditions linear search does not have — the values must be ordered, the container must support jumping to the middle, and any insert must keep the order — and on these seven elements that overhead is still visible in the counts below.

The same inputs, counted

These numbers come from running both real implementations: comparisons, on the sorted array 2, 5, 8, 12, 16, 23, 38.

InputLinear searchBinary search
Target is the first element15
Target in the middle41
Target is the last element75
Target is not in the array76

What the numbers show

Seven elements is small enough that halving has barely started to pay. Looking for 12, the value that happens to sit exactly in the middle, binary search finds it in 1 comparison against linear search's 4: it tests the midpoint and is done. Looking for 2, the first element, the ranking inverts — linear search needs 1 comparison, binary search 5, because 2 is the furthest value from every midpoint it will ever test and the range has to shrink all the way down to it.

The honest summary of this array is that neither one dominates. For the last element, 38, linear search makes 7 comparisons and binary search 5. When the target is absent — the page looks for 10, which falls between 8 and 12 — linear search must prove it by exhausting the array, 7 comparisons, while binary search narrows to an empty range in 6. That is a difference of one. At n = 7, log₂ n is under 3, so the asymptotic O(log n) against O(n) gap has almost no room to show itself.

Scale is where the choice is actually decided. The table's own worst column states it: O(n) against O(log n). On a thousand sorted values linear search averages about five hundred comparisons and binary search needs at most ten; on a million, about half a million against twenty. Read these seven-element counts as a demonstration that halving is not magic on small inputs, not as evidence against it — and note that binary search only gets to make its 1-comparison best case because something already paid to sort the array.

See them run