Linear Search vs Binary Search

Across our 4 test inputs, Binary Search needed 17 comparisons in total versus 42 for Linear Search. On average Linear Search runs in O(n) time and Binary Search in O(log n). Binary Search only works on sorted data.

Head-to-head on identical inputs (comparisons)

InputLinear SearchBinary Search
Target is first14
Target in the middle94
Target is last165
Target absent164

Complexity

Linear SearchBinary Search
Average timeO(n)O(log n)
Worst timeO(n)O(log n)
SpaceO(1)O(1)
StableYesYes

When to use Linear Search

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

When to use Binary Search

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