algorithm
binary search
ternary search
computer science
search techniques

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.

python
1def binary_search(arr, target):
2    lo, hi = 0, len(arr) - 1
3    while lo <= hi:
4        mid = lo + (hi - lo) // 2
5        if arr[mid] == target:
6            return mid
7        if arr[mid] < target:
8            lo = mid + 1
9        else:
10            hi = mid - 1
11    return -1

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.

python
1def ternary_search(arr, target):
2    lo, hi = 0, len(arr) - 1
3    while lo <= hi:
4        third = (hi - lo) // 3
5        m1 = lo + third
6        m2 = hi - third
7
8        if arr[m1] == target:
9            return m1
10        if arr[m2] == target:
11            return m2
12
13        if target < arr[m1]:
14            hi = m1 - 1
15        elif target > arr[m2]:
16            lo = m2 + 1
17        else:
18            lo = m1 + 1
19            hi = m2 - 1
20    return -1

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.

python
1def ternary_search_peak(f, lo, hi, iters=100):
2    for _ in range(iters):
3        m1 = lo + (hi - lo) / 3
4        m2 = hi - (hi - lo) / 3
5        if f(m1) < f(m2):
6            lo = m1
7        else:
8            hi = m2
9    return (lo + hi) / 2
10
11peak = ternary_search_peak(lambda x: -(x - 3) ** 2 + 10, -100, 100)
12print(round(peak, 5))

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.

Course illustration
Course illustration

All Rights Reserved.