Linear Search vs Ternary Search

Across our 4 test inputs, Ternary Search needed 28 comparisons in total versus 42 for Linear Search. On average Linear Search runs in O(n) time and Ternary Search in O(log₃ n). Linear Search is stable; Ternary Search is not. Ternary Search only works on sorted data.

Head-to-head on identical inputs (comparisons)

InputLinear SearchTernary Search
Target is first17
Target in the middle95
Target is last167
Target absent169

Complexity

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

When to use Linear Search

Unsorted arrays, small datasets, or when you need to find all occurrences.

When to use Ternary Search

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