Ternary Search
Splits the range into thirds and uses two midpoints (mid1, mid2) to discriminate three sections at once. Its recursion is shallower than binary search but it does more comparisons per step, so in practice it's mainly used to find the extremum of a unimodal function.
01Ternary Search
Explore How It WorksStarting ternary search. Split the range into thirds, compare two points at once, and keep the one section that can hold the target.
Splitting ten slots into thirds puts the two boundaries at index 3 and 6. Check both at once.
The boundary values are 34 and 67. 45 falls between them, so keep the middle section and drop both sides.
Repeat on the two remaining slots. The boundaries are index 4 and 5.
The front boundary holds 45 — a match, after splitting the range twice.
Search complete · 45 sits at index 4. Two comparisons per split make it slower than binary search in practice; it is mainly used to find the extremum of a unimodal function.
02 Understand It Simply
For EveryoneSplits the range into three, checks two points and discards one third. Used to find the extremum of a unimodal function.
Divides the range into thirds and narrows using two points.
Used less for sorted search and more for finding the extremum of a convex (unimodal) function.
- –Finding the optimum of a unimodal function
03 Python Implementation
A clean, readable reference implementation of the core logic of Ternary Search.
04 Frequently Asked Questions
FAQWhat is Ternary Search?+
Splits the range into thirds and uses two midpoints (mid1, mid2) to discriminate three sections at once. Its recursion is shallower than binary search but it does more comparisons per step, so in practice it's mainly used to find the extremum of a unimodal function.
What is the time complexity of Ternary Search?+
The time complexity of Ternary Search is O(log₃ n). Follow the step-by-step visualization to see exactly why.
Where is Ternary Search used?+
Finding the optimum of a unimodal function.
What's a simple analogy for Ternary Search?+
Splits the range into three, checks two points and discards one third. Used to find the extremum of a unimodal function.
