If Binary Search splits the search space in two, Ternary Search splits it in three — using two midpoints and discarding one third of the range at a time.
The surprise: this is not faster — fewer rounds, but more comparisons per round. Ternary Search’s real home isn’t array lookup at all. It’s finding the peak of a unimodal function, where two midpoints let you decide which side of the peak you’re on.
How It Works
- Divide the interval into three equal parts using two midpoints:
mid1andmid2. - If the target equals either midpoint, return it.
- If the target is smaller than
mid1, search the leftmost third. - If the target is larger than
mid2, search the rightmost third. - Otherwise, search the middle third between the midpoints.
- Repeat until the interval is empty or the target is found.
Key Insight
For sorted-array search, Ternary Search is mathematically worse than binary search.
- Ternary round: two comparisons, removes
1/3of the range. - Binary round: one comparison, removes
1/2of the range.
log₂(n) vs 2·log₃(n) ≈ 1.26·log₂(n) — ternary search always does about 26% more work.
But for unimodal functions (a single peak, like a mountain), ternary search is the classic tool. Evaluate the function at both midpoints — the smaller (for minima) or larger (for maxima) value tells you which third can’t contain the optimum.
Worked Example
Search for 6 in [1, 3, 4, 6, 8, 9, 11, 12, 15, 17]:
[0, 9]:mid1=3(6),mid2=6(11).arr[3]=6→ return 3
Search for 7:
[0, 9]:mid1=3→6 < 7;mid2=6→11 > 7→ middle third[4, 5][4, 5]:mid1=4→8 > 7→ left third[4, 3]— empty → return -1
In the visualizer, two midpoints highlight simultaneously and the range snaps to one of the three thirds.
Edge Cases & Pitfalls
- Empty interval — return -1 before computing midpoints.
- Target exactly at a midpoint — caught directly: the base case.
mid1 == mid2— on tiny intervals the midpoints collide. Handle gracefully by checking the remaining one or two elements.- Don’t confuse sorted search with optimization — for plain lookup, binary search is strictly better. Use ternary search only for unimodal optimization, not matching.
- Non-unimodal functions — three-way splitting gives no correctness guarantee when multiple local peaks exist.
Comparison With Other Searches
| Scenario | Ternary Search | Binary Search | Linear Search |
|---|---|---|---|
| Comparisons (sorted) | 2·log₃(n) ≈ 1.26·log₂(n) | log₂(n) — fewer | O(n) |
| Unimodal optimization | Best tool | Not applicable | O(n) sampling |
| Requires sorted input | Yes (for lookup) | Yes | No |
Applications
- Finding the maximum/minimum of a unimodal function (peak detection, resource allocation curves)
- Peak finding in arrays that increase then decrease (mountain arrays)
- Educational contrast with binary search — teaches why splitting into more than two parts doesn’t help for lookup
Practice Trajectory
- Hand-trace Ternary Search for target 9 in
[2, 4, 6, 8, 9, 10, 12, 14], showing both midpoints each round. - Prove that ternary search needs more comparisons than binary search for the same array.
- Use ternary search to find the maximum of
f(x) = -x² + 10xon[0, 10]. - Explain why two midpoints are necessary for unimodal optimization.
- Implement ternary search for a lookup, then adapt it to a maximization routine.