Searching Algorithms

How Linear Search Works

Linear search checks each element in turn. It is the only search that needs no order at all, and the baseline every other search has to beat.

158
index 016 of 16 still possible15
  • Ruled out
  • Still possible
  • Checked now
  • Found
Speed

Step 1 of 15

Looking for 158 in 16 sorted values, from 4 to 196.

Counters

Steps
0
Result
-
Still possible
16
of 16
Worst case
16
steps for this size

Settings

Is it in the array?
Gaps between values

Two to 64 numbers. Sorted ascending before the search runs.

Linear search

  1. 1for i in 0..n-1:
  2. 2 if a[i] == target:
  3. 3 return i
  4. 4 if a[i] > target:
  5. 5 return NOT_FOUND // sorted
  6. 6return NOT_FOUND

The same array, every algorithm

Probes on the values above, cheapest first. One probe is one value actually looked at.

  1. Interpolation1
  2. Ternary3
  3. Binary4
  4. Jump5
  5. Exponential8
  6. Linear13

How the six compare

AlgorithmBestAverageWorstNeeds a sorted array
LinearO(1)O(n)O(n)No
BinaryO(1)O(log n)O(log n)Yes
TernaryO(1)O(log n)O(log n)Yes
JumpO(1)O(√n)O(√n)Yes
ExponentialO(1)O(log n)O(log n)Yes
InterpolationO(1)O(log log n)*O(n)Yes

Interpolation search only reaches that average when the gaps between the values are even.

Complexity

BestAverageWorstExtra memory
O(1)O(n)O(n)O(1)

On average it looks at half the list when the value is present, and all of it when it is not.

How linear search works

Linear search walks the list from one end, comparing each element with the target, and stops at the first match. It makes no assumptions: the data need not be sorted, indexed or even random-access — a linked list or a stream works the same way. That generality is the whole point, and the cost is that it has no way to skip anything.

  1. Start at the first element.
  2. Compare it with the target. If they match, return the index.
  3. Otherwise move to the next element.
  4. If the end is reached without a match, the value is not present.

Implementation

The same algorithm the visualiser is running, written to be read.

def linear_search(values, target):
    for i, value in enumerate(values):
        if value == target:
            return i
    return -1

When to use it

  • Use it on unsorted data, which is most data. Sorting a list to binary search it once costs more than scanning it.
  • Use it on small collections, where the constant factors dominate and a scan of twenty elements beats any cleverness.
  • Use it when the data arrives as a stream and you cannot jump around it at all.
  • Avoid it on large sorted collections you search repeatedly — that is precisely what binary search is for.

Check yourself

If the animation made sense, these should too.

When does searching a sorted array with a linear scan beat binary search?

When the array is small enough that the constant factors dominate, when the data cannot be randomly accessed - a linked list or a stream - and when the collection would have to be sorted first: sorting is O(n log n), so a one-off search should just scan.

Why is the average cost n/2 for a hit but n for a miss?

A hit stops at the target, which is on average halfway along if every position is equally likely. A miss has no early exit - it has to look at every element to prove the value is absent.

More searching algorithms