Binary Search vs Ternary Search

Across our 4 test inputs, Binary Search needed 17 comparisons in total versus 28 for Ternary Search. On average Binary Search runs in O(log n) time and Ternary Search in O(log₃ n). Binary Search is stable; Ternary Search is not.

Head-to-head on identical inputs (comparisons)

InputBinary SearchTernary Search
Target is first47
Target in the middle45
Target is last57
Target absent49

Complexity

Binary SearchTernary Search
Average timeO(log n)O(log₃ n)
Worst timeO(log n)O(log₃ n)
SpaceO(1)O(1)
StableYesNo

When to use Binary Search

Sorted arrays when O(log n) search time is needed. Foundation for many advanced algorithms.

When to use Ternary Search

Finding the peak/valley of a unimodal function; for plain sorted lookups binary search is usually better.