SearchingAnimated
Ternary Search
Split a sorted range in three and discard two thirds each step.
Time
O(log₃ n)
Extra space
O(1)
Use it for
Unimodal functions; rarely beats binary search on arrays.
How it works
- On a sorted list, pick two probe points that split the window into three parts.
- If the target equals either probe, stop.
- If it is smaller than the first probe, keep the left third; if larger than the second, keep the right third; otherwise keep the middle third.
- Repeat until found or the window is empty.
- It does about 2·log₃ n comparisons, which is slightly worse than binary search in practice.
Watch it run
Loading animation…