Short answer: Ternary search finds the minimum of a unimodal function by comparing the function at the one-third and two-third points and discarding the third that cannot hold the minimum. Each step keeps 2/3 of the interval, so the number of evaluations is Θ(log n), with two evaluations per step. On a discrete unimodal array, comparing two neighbors and binary-searching the slope uses one evaluation per step and shrinks the range by half.
What the problem actually asks
The function falls and then rises. Any point to the left of the minimum has a neighbor that is smaller on its right. Any point to the right has a neighbor that is smaller on its left. Two interior samples, ordered along the line, say which side of the minimum the search is on: the sample with the larger value is the side that can be discarded, out through that sample.
This is not a faster way to find a key in a sorted array. Equality search already has binary search, which discards half the range with one comparison. Two comparisons that discard only one third are more work.
What the algorithm guarantees
On a continuous unimodal function, or on a discrete array that decreases and then increases, the larger of the two samples can be excluded, and the minimum remains inside the kept interval. After t steps the interval is (2/3)^t of the original, so Θ(log(1/ε)) evaluations reach a tolerance ε, and Θ(log n) evaluations finish a discrete range of length n. Extra memory is a few endpoints.
If the function is only weakly unimodal, with a flat bottom, the same discards retain at least one minimum, and they may retain a whole flat run. A strictly unimodal function, decreasing up to a unique minimum and increasing after it, ends with that unique index. Golden-section search is the continuous variant that reuses one sample at the next step so only one new evaluation is required. The discard ratio is then the golden-section conjugate, about 0.618, which is a smaller shrink than 2/3 per evaluation only in the accounting that charges the reused point fairly. On a discrete array the neighbor comparison is simpler and discards half.
The working steps
- Start with the full interval [lo, hi], and with a function that is unimodal on it.
- Set m1 and m2 one third and two thirds of the way from lo to hi. On an integer index range, use lo + (hi−lo)//3 and hi − (hi−lo)//3, and stop when the width is small.
- If f(m1) ≤ f(m2), the minimum is at or left of m2. Set hi = m2. Otherwise set lo = m1.
- Repeat until the interval is a single index or is within the required tolerance.
- On a discrete array, prefer the slope test: if a[mid] ≤ a[mid+1], the minimum is at or left of mid; otherwise it is at or right of mid+1. That is binary search and uses one probe.
The discard is valid for a unimodal function. A function with two valleys can hide the global minimum in the third that was dropped. A sorted array searched for a key is binary search. Nothing in the 2/3 shrink beats a 1/2 shrink that costs one comparison.
Approaches that look simpler and fail
| Approach | Why it fails when the function is not unimodal |
|---|---|
| Two valleys | The samples can both lie on the slopes of the shallower valley. The deeper valley is discarded with the third that contained it. |
| Ternary search for a key in a sorted array | Each step spends two comparisons to keep 2/3 of the range. Binary search spends one comparison to keep 1/2, and finishes in fewer comparisons. |
| Assume the discrete neighbor test is ternary search | Comparing a[mid] with a[mid+1] is binary search on the slope. It is the better discrete algorithm and a different procedure. |
| Reuse both sample points after the interval moves | One of the two samples falls outside the kept interval or no longer sits at a third. Golden-section search is the reuse pattern that stays consistent, and plain ternary search recomputes both. |
Equality search on a sorted array belongs to binary search. A continuous unimodal search that must reuse evaluations belongs to golden-section search. The discrete slope test belongs with binary search.
A working example
This Python fragment narrows a unimodal function with two probes and keeps the third that contains the minimum. f(x) = (x−3)² on the integers 0…6 has its minimum at 3.
def f(x):
return (x - 3) ** 2
lo, hi = 0, 6
while hi - lo > 2:
left = lo + (hi - lo) // 3
right = hi - (hi - lo) // 3
if f(left) < f(right):
hi = right
else:
lo = left
best = min(range(lo, hi + 1), key=f)
assert best == 3
print(best)
Where it is used
Ternary search finds the maximum of a unimodal function, such as a latency curve with a single valley, by dropping one third of the domain. Golden-section search is the continuous relative that libraries use more often. Newton’s method needs a derivative.
When you can drop this algorithm
Drop ternary search on a discrete unimodal array in favor of the one-comparison neighbor test. Drop it entirely when the function may have more than one valley; a global optimization method is then required, and this discard is unsafe. Keep the two-probe ternary split for a unimodal function when both new evaluations are cheap to describe and the interval is continuous.
Why this stays published
We publish this so a search for ternary search returns the unimodal hypothesis and the reason it is the wrong tool on a sorted array. Ternary search discards one third of a unimodal interval after two evaluations, and a discrete unimodal array is better searched by comparing neighbors.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
