Why use binary search if there's ternary search?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Binary search is usually preferred over ternary search for searching sorted arrays, even though ternary search splits the range into three parts. The reason is not just asymptotic notation but total comparison cost per iteration. In practice, binary search does less work per step and is simpler to implement correctly.
Binary and Ternary Search Do Different Amounts of Work per Step
Binary search checks one midpoint and discards half of the range.
Ternary search checks two midpoints and discards about two-thirds of the range.
At first glance, reducing to one-third seems better, but each ternary iteration performs more comparisons and arithmetic.
This loop uses one midpoint and up to two comparisons per iteration.
Comparison Count Intuition
Asymptotically:
- Binary search iterations scale with
log2(n). - Ternary search iterations scale with
log3(n).
But ternary search does two midpoint checks each iteration, so constant factors often erase the iteration advantage.
For sorted-array lookup on modern hardware, binary search usually wins on simplicity and lower per-iteration overhead.
Practical Engineering Factors
Binary search advantages in production code:
- Less branching complexity.
- Easier boundary correctness.
- Smaller bug surface for off-by-one errors.
- Familiar pattern across languages and standard libraries.
Most standard libraries optimize binary search heavily, making custom ternary search unnecessary for typical sorted arrays.
Where Ternary Search Is Actually Useful
Ternary search is valuable in another context: optimizing unimodal functions on numeric domains.
If a function increases then decreases, ternary search can find maximum points efficiently without derivatives.
That use case is different from searching in sorted discrete arrays.
Why Binary Search Is the Default in Sorted Data Structures
In array lookup, cost model includes:
- Comparisons
- Branch prediction behavior
- Arithmetic instructions
- Cache and memory access patterns
Binary search keeps this cost profile lean. Ternary search adds extra comparisons and branch paths per loop, which often slows real workloads despite theoretical base-3 shrinkage.
Decision Rule
For sorted array membership or index retrieval:
- Use binary search.
For unimodal function optimization:
- Use ternary search over numeric interval.
For tree and index structures:
- Use domain-specific search logic already built into the structure.
Choosing by problem type matters more than memorizing one algorithm as universally superior.
Common Pitfalls
- Applying ternary search to sorted arrays expecting automatic speedup.
- Confusing ternary search for unimodal optimization with array lookup ternary partitioning.
- Overlooking implementation complexity and boundary bugs.
- Benchmarking with tiny inputs and drawing broad conclusions.
- Reimplementing search loops instead of using tested standard library functions.
Summary
- Binary search is usually better for sorted-array lookup.
- Ternary search reduces range faster per iteration but costs more comparisons each step.
- Constant factors and implementation simplicity favor binary search in practice.
- Ternary search is excellent for unimodal function optimization.
- Match algorithm to problem structure, not just logarithm base intuition.

