Searching Algorithms — Overview
Every algorithm in this folder answers the same question — "is this value present, and where" — but each one pays for the answer differently. Linear search pays nothing up front and every time it runs. Binary search pays once, up front, to sort the data, and on every lookup after that. A hash table pays to build and expected per lookup, at the cost of any ordering.
None of these is "the best search algorithm" in the abstract. The right choice is a question about your workload — how many times you look something up before the data changes underneath you — not a question about which algorithm is cleverest. This page works that trade-off out with actual arithmetic, because "sorting is worth it if you search often enough" is true but useless until you know what "often enough" means for your n.
In This Section
- Linear Search — check each element. No preconditions, no preparation, and the only option when there is no ordering to exploit.
- Binary Search — halve the search space each step. Requires sorted, random-access data, and is notoriously easy to get subtly wrong at the boundaries.
- Binary Search on the Answer — the same halving idea applied to a range of candidate answers instead of an array, for problems phrased as "find the smallest/largest value for which a condition holds."
- Exponential & Ternary Search — exponential search for unbounded or streamed input where the size is not known in advance, and ternary search for finding an extremum of a unimodal function rather than a target value.
The Options, Compared
| Approach | Preparation | Per lookup | Requires | Also gives you |
|---|---|---|---|---|
| Linear search | None | Nothing | Works on any sequence, any predicate | |
| Binary search | sort | Sorted, random access | Range queries, nearest match, insertion point | |
| Hash table | build | expected | Hashable keys | Nothing else — no ordering |
| Balanced BST | build | Comparable keys | Ordering, ranges, and cheap updates |
The Cost of the Precondition
Binary search's lookup is not free — it is a rate you buy by paying an entrance fee to sort first. Whether that trade is worth it depends entirely on , the number of lookups you intend to run before the data changes again.
Compare the two totals directly, for elements and queries:
Sorting wins exactly when the second expression is smaller than the first. Solve for the break-even point :
For any large enough that (true for essentially every worth searching), the term in the denominator barely moves the answer, and . The break-even point is, to a very good approximation, one query per bit of the data's size — a genuinely small number of queries, which is why binary search's up-front cost so rarely dominates in practice.

This is a genuinely common performance bug. Sorting costs and a single binary search saves at most over a linear scan, so re-sorting on every lookup is strictly worse than never sorting at all — you pay the full entrance fee every time and use it once. Sort once outside the loop, or reach for a structure that stays sorted incrementally (balanced BST) if the data keeps changing.
Worked Example: n = 1,000
Plugging into the exact break-even formula:
log2(1000) ≈ 9.9658
q* = (1000 * 9.9658) / (1000 - 9.9658)
= 9965.8 / 990.0342
≈ 10.066
So for a thousand elements, the eleventh query is where sorting-then-binary-searching becomes cheaper in total than eleven separate linear scans — check the arithmetic directly:
q = 10 linear: 10 * 1000 = 10,000
sorted: 1000*9.9658 + 10*9.9658 = 9965.8 + 99.658 = 10,065.5 (sorted still costs more)
q = 11 linear: 11 * 1000 = 11,000
sorted: 1000*9.9658 + 11*9.9658 = 9965.8 + 109.62 = 10,075.4 (sorted now wins)
Ten queries: linear search is still cheaper in total. Eleven queries: sorting has paid for itself. This is also why "sort once, binary search times" is the textbook justification for treating as the rule of thumb, and why it holds up almost regardless of — doubling to two million only moves the break-even point from about 10 to about 21.
Deciding
Recall
References
- Knuth, The Art of Computer Programming, Vol. 3, §6.1 (sequential search) and §6.2.1 (binary search) — the two baselines this page compares, with their exact comparison counts.
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed., §12.1 — binary search trees as the incrementally-updatable alternative to sort-once-then-binary-search.
- Sedgewick & Wayne, Algorithms, 4th ed., §3.1 "Symbol Tables" — frames search structures by exactly this cost trade-off: preparation cost versus per-query cost versus update cost.
Related Pages
- Hash Tables — the option, and its conditions.
- Balanced Trees — when the data keeps changing.
- Sorting Algorithms — the preparation step binary search depends on.