AlgoLab

O(n)·O(1) space
511181928314243454851515563647377969899

Search runs on a sorted copy of your array, because binary and jump search need sorted input. The bars are that sorted copy.

Searching a sorted array of 20 items for 51, one position at a time.

Searching a sorted array of 20 items for 51, one position at a time.
  1. 1compare the value at the current position with the target
  2. 2if it matches, the target is found
  3. 3the target is not in the array
Comparisons
0
Writes
0
Accesses
0

Complexity

BestO(1)
AverageO(n)
WorstO(n)
Extra spaceO(1)
n at n=20 predicts 20; this run took 0.

Linear search

Linear search checks each value in the array in order, comparing it with the target, until it finds a match or runs out of values.

When it is the right choice

Linear search is the right choice for unsorted data or a one-off search, since it needs no preparation and works on any array. It is a poor choice for repeated searches on the same large array, where sorting once and using binary search pays for itself quickly.

How it works

Worth noticing

Cost

Best
O(1)
Average
O(n)
Worst
O(n)
Extra space
O(1)

Worth comparing

All algorithms