Don't memorize ternary search — watch the window shrink. When a curve rises to a single peak and falls, two probes at the one-third marks are enough to know which outer third cannot hold the top. Throw that third away, keep two-thirds, repeat. The window collapses onto the peak in O(log n). It is binary search's cousin — but it needs a unimodal shape, not a monotonic one.
The curve has a single peak at x = 7. The amber band is the live window [lo, hi]. Press Step: two probes appear at the thirds, and the third that cannot hold the peak turns red and is dropped.
m1 = lo + (hi − lo)/3 m2 = hi − (hi − lo)/3
12.0
Window width
—
Peak x ≈
Two probes beat one midpoint
Binary search works because the data is monotonic: check the middle, and one comparison rules out an entire half. A unimodal curve breaks that — the middle might be on the way up or on the way down, and a single value cannot tell you which. So ternary search takes two samples, m1 at the one-third mark and m2 at the two-third mark. If f(m1) < f(m2), the peak cannot lie left of m1 (the curve is still climbing toward m2), so you set lo = m1 and drop the left third. If f(m1) > f(m2), symmetric: set hi = m2. Either way two-thirds survives, and the window keeps closing on the maximum.
01
Require a single peak
The function (or bitonic array) must rise then fall exactly once. On arbitrary data ternary search is meaningless.
02
Probe at the thirds
m1 = lo + (hi − lo)/3, m2 = hi − (hi − lo)/3. Evaluate f at both.
03
Drop the losing third
For a max: if f(m1) < f(m2) set lo = m1; else set hi = m2. The discarded third can't contain the peak.
04
Shrink to a sliver
Each step keeps ~2/3 of the range, so after O(log n) iterations lo and hi meet on the peak.
Time
O(log n)
Space
O(1)
Needs
Unimodal
Per step
2 evals
The window shrinks by a factor of 2/3 each iteration, so the count of steps grows like log base 1.5 of n — the same logarithmic class as binary search, just with two function evaluations per step instead of one. Reach for ternary search whenever you are optimizing a unimodal quantity: the peak of a projectile, the best meeting point on a line, or the minimum of a convex cost. When the data is merely sorted or a predicate is monotonic, plain binary search is the simpler, faster tool.
The picture: a window closing on the tallest bar
A bitonic array of 18 values with its peak (43) at index 9. Each line under the bars is the search window at the start of one step. The window never leaves the peak, and it is one cell wide after six steps.
Worked example, step by step
Search the array [2, 5, 9, 14, 20, 27, 33, 38, 41, 43, 40, 35, 28, 19, 11, 6, 3, 1] with the page’s loop: m1 = lo + (hi − lo) // 3, m2 = hi − (hi − lo) // 3, and if nums[m1] < nums[m2] set lo = m1 + 1, otherwise hi = m2 − 1.
Step
Window
m1
m2
Compare
Action
1
0–17
5
12
27 < 28
drop left: lo = 6
2
6–17
9
14
43 > 11
drop right: hi = 13
3
6–13
8
11
41 > 35
drop right: hi = 10
4
6–10
7
9
38 < 43
drop left: lo = 8
5
8–10
8
10
41 > 40
drop right: hi = 9
6
8–9
8
9
41 < 43
drop left: lo = 9
7
9–9
window is one cell: peak index 9, value 43
Six steps, twelve array reads, and the window ends on index 9. Look at step 3: nums[8] = 41 is bigger than nums[11] = 35, so the peak cannot be right of index 11, and the window drops from 6–13 to 6–10. At step 6 the probes are 8 and 9, adjacent cells, which is the end game: when the two thirds are only one cell apart you are comparing neighbours, as in binary search. A binary search on the slope (nums[mid] < nums[mid+1]) finds the same index with 10 reads, not 12. On a synthetic unimodal sequence of one billion values the counts are 98 reads for ternary against 60. Ternary search is the right tool when you cannot compare neighbours, as with a continuous function; for an array, the slope search is cheaper.
Common mistakes
Assuming plateaus are fine
The method needs a strictly rising then strictly falling shape. A flat spot on the rise breaks it: on [1, 2, 2, 3] the probes land on the two equal 2s, the code treats equal as “peak is left” and returns index 1 (value 2), not index 3 (value 3).
Using <code>lo = m1</code> or <code>hi = m2</code> on integers
When the window is two cells wide, m1 = lo and m2 = hi, so setting lo = m1 or hi = m2 makes no progress and the loop never ends. [1, 2] hangs on lo = m1 and [5, 3] hangs on hi = m2. Step past the probe: lo = m1 + 1, hi = m2 − 1.
Using it for a minimum without flipping the comparison
On the valley [9, 5, 2, 1, 3, 7] the maximum-finding rule returns index 0 (value 9), not index 3 (value 1). For a minimum, reverse the comparison.
Fractional indices
In languages where / is true division, lo + (hi − lo) / 3 is not an integer for most windows. For lo = 0, hi = 7 it is 2.33, and indexing an array with it gives undefined in JavaScript. Floor the thirds on integer domains.
Stopping a continuous search on equality
On real numbers the probes are almost never equal, and the window shrinks by 2/3 per step. From a width of 12, 58 steps (116 evaluations) bring it under 1e-9. Loop a fixed number of times, or until the width is below a tolerance, never until lo == hi.
Where this shows up
Minimising a convex cost
Any one-variable cost that falls then rises can be minimised by ternary search with the comparison flipped, for example the best meeting point on a line, or the time at which two moving objects are closest.
Line search in optimisation
Tuning a single scalar, such as a step size or a threshold, over a bracketed range is a common use. Golden-section search is the refined form: it reuses one probe each step, so each evaluation shrinks the range by 0.618, against √(2/3) ≈ 0.816 for ternary.
Bitonic arrays
Rotated or mountain-shaped data, such as a series that climbs then falls, can be searched for its extreme. Prefer the neighbour-comparison binary search when the data is an array.
Programming contests
It is a frequent trick when the cost curve is known to be unimodal but has no closed form, and the answer must be an integer or a real to a tolerance.
Complexity and trade-offs
Case
Time
Space
Why
Ternary search
O(log n)
O(1)
Two reads per step, window keeps 2/3. On a unimodal sequence of 1,000,000 values: 64 reads; of 1,000,000,000: 98.
Binary search on the slope
O(log n)
O(1)
Compare nums[mid] with nums[mid+1], halve the window. Same sequences: 40 and 60 reads. Arrays only.
Golden-section search
O(log n)
O(1)
Reuses one probe per step, so one new evaluation each. Shrinking [0, 12] to under 1e-9 on the page curve took 51 evaluations, against 116 for ternary.
Linear scan
O(n)
O(1)
Needs no shape assumption: 1,000 values means 1,000 reads, where ternary needed 30.
All three searches are logarithmic, so the difference is the constant. Ternary pays two evaluations to cut a third, which is the worst ratio of the three: 0.816 of the range per evaluation against 0.707 for halving with two reads, and 0.618 for golden-section. It earns its place only where the slope is unavailable or costly, as with an expensive black-box function. Everything here assumes a strictly unimodal shape; on anything else the linear scan is the only one that is still correct.
SOLVE IT YOURSELF
Solve it: find the peak
This one is yours to write — in Python or TypeScript, running for real in your browser. Edit the stub below, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are below and Reveal solution is one click away.
YOUR TASK
A bitonic array strictly increases, then strictly decreases. Implement peak_index(nums): return the index of the maximum using ternary search — split the range into thirds with two probes and drop the third that cannot hold the peak. O(log n).
HINTS — 4 IDEAS
Keep a window [lo, hi]. Take two probes at the thirds: m1 = lo + (hi−lo)//3 and m2 = hi − (hi−lo)//3.
If nums[m1] < nums[m2] you are still climbing at m1, so the peak is to its right: lo = m1 + 1.
Otherwise the peak is at or left of m2: hi = m2 − 1. (Using m2 − 1, not m2, keeps the window shrinking.)
When lo == hi the window is one element — that index is the peak.
CPython · WebAssembly✓ Solved
1
1
▶ Watch it explained
Ternary Search: Time and Space Complexity, From First Principles
Check yourself
1 · What property must the function have for ternary search to work?
Ternary search needs exactly one extremum: the curve rises then falls (max) or falls then rises (min). That is what makes a third safe to drop.
2 · Searching for a maximum, you find f(m1) < f(m2). What do you do?
Since m2 is higher, the curve is still climbing past m1, so the peak is to the right of m1. The left third [lo, m1] can be discarded: lo = m1.
3 · Why can't you just use binary search here?
Binary search relies on a monotonic order. A unimodal curve isn't monotonic, so one comparison at the middle is ambiguous — you need two probes to read the slope.
Questions
Ternary vs binary search — which is faster?
Same big-O class. Binary search keeps 1/2 with one evaluation per step; ternary keeps 2/3 with two evaluations. So ternary needs a few more iterations and twice the evals per iteration — a constant-factor cost, not an asymptotic one. Use it only when the data is unimodal rather than monotonic.
How do I handle an integer domain?
The 1/3 splits can stall on the last two or three integers because of rounding. A safe pattern is to loop while (hi − lo > 2) and then check the remaining handful directly, or compare f(mid) with f(mid + 1) to decide which half of the integer range keeps climbing.
Can it find a minimum too?
Yes — for a convex, single-valley function just flip the comparison: keep the side with the smaller value. The structure is identical; only the direction of the test changes.
Two probes, and the peak has nowhere to hide.
Split into thirds, drop the third that can't win, keep two-thirds — the window closes on the maximum in log time.