# Ternary search visualised: three thirds instead of two halves

> Interactive ternary search: splits the range at m1 and m2 into three parts. O(log₃ n), asymptotically like binary search, typical for unimodal functions.

Address: https://marckimmel.de/en/informatik/suchalgorithmen/ternaere-suche
Deutsche Fassung: https://marckimmel.de/informatik/suchalgorithmen/ternaere-suche.md

Splits the search range into three thirds and compares at the two split points.

Ternary search is a direct generalisation of binary search that splits the current range into three parts instead of two. With m1 = lo + ⌊(hi − lo) / 3⌋ and m2 = hi − ⌊(hi − lo) / 3⌋ it compares two points per iteration. If the target lies to the left of a[m1] it continues in the left third; right of a[m2] in the right third; otherwise in the middle third between m1 and m2. Runtime is O(log₃ n), asymptotically equivalent to binary search. But ternary search makes two comparisons per iteration instead of one, so it is slower in practice and rarely used as a pure search. Its main use is as an optimisation tool for unimodal (strictly increasing then decreasing) functions.

Classic application: finding the maximum of a unimodal function, one that rises and then falls. Examples include the peak of a parabola or the optimal throw range of a projectile. As a pure array search, inferior to binary search.

## Related

- [Search Algorithms](https://marckimmel.de/en/informatik/suchalgorithmen.md)
