Skip to content
StackDecode
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

  1. On a sorted list, pick two probe points that split the window into three parts.
  2. If the target equals either probe, stop.
  3. 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.
  4. Repeat until found or the window is empty.
  5. It does about 2·log₃ n comparisons, which is slightly worse than binary search in practice.

Watch it run

Loading animation…