Linear Search

Searching ・ O(n)

Compares values one by one from the left to find the target.

Move it step by step

Searching array [3, 8, 12, 17, 23, 31, 42, 56] for 42

  1. 3
    0
  2. 8
    1
  3. 12
    2
  4. 17
    3
  5. 23
    4
  6. 31
    5
  7. 42
    6
  8. 56
    7
  • comparing
  • found
  • different
  • The number under each box is its index

Step 1 of 9Comparisons: 0

We're looking for 42. We'll check from the leftmost index, 0, one by one.

The pseudocode line currently running

  1. 1for i from 0 to n - 1:
  2. 2if a[i] == x:
  3. 3return i (found)
  4. 4return -1 (not found)

Change the example

Try predicting

You're on step 1.
Which index will be compared next?

What you'll learn

  • The flow of searching an array by comparing values one by one from the left
  • Why the number of comparisons grows in proportion to the array's length

Prerequisites

  • Arrays and indices (numbering that starts at 0)
  • Writing a for loop that takes out one value at a time

How this search works

Linear search compares values in order from the left end of the array and stops as soon as it finds one equal to the target.

It works no matter how the array is ordered. The tradeoff is that, if you're unlucky, it compares every single value before finishing.

Steps, with an example

  1. 1Compare the value at index 0 with the target value
  2. 2If they match, that index is the answer, and we're done
  3. 3If they differ, move one step to the right and repeat
  4. 4If we reach the right end without a match, we finish with "not found"
Searching for 42 (found in 7 comparisons)
#Compared atResult
1Index 0: 3Different. Move right
2Index 1: 8Different. Move right
3Index 2: 12Different. Move right
4Index 3: 17Different. Move right
5Index 4: 23Different. Move right
6Index 5: 31Different. Move right
7Index 6: 42Same. Found

Comparisons and complexity

CaseComparisonsComplexity
Best case1 comparison (value is at the left end)O(1)
AverageAbout n ÷ 2 comparisonsO(n)
Worst casen comparisons (value is at the right end, or not present)O(n)

n is the length of the array. With 8 elements, the worst case is 8 comparisons; with 1,000,000 elements, it's 1,000,000 comparisons.

Comparisons for the same array [3, 8, 12, 17, 23, 31, 42, 56]
TargetLinear searchBinary search
4273
2083
313
5684

When to use it

Good fit

  • The array isn't sorted
  • The array is small, with up to a few dozen elements
  • You're only searching once, so sorting first would cost more than it saves

Not a good fit

  • A large array you'll search many times (use binary search instead)
  • You want to look values up directly by key (use a dictionary or hash table instead)

Code

Pseudocode

  1. 1for i from 0 to n - 1:
  2. 2if a[i] == x:
  3. 3return i (found)
  4. 4return -1 (not found)

Working code

pythonRunnable
Output

▶ Press “Run” to see the result here

Edit the Python or JavaScript and press “Run” to see the result right away. Java is read-only here.

FAQ

How is this different from Python's `in` or `.index()`?
Both `x in a` and `a.index(x)` compare values from the left internally, the same way. Writing linear search yourself makes that work visible.
If the same value appears twice, which index is returned?
Since we compare from the left, the leftmost matching index is returned first.
Why return -1 when the value isn't found?
Since indices are always 0 or greater, -1 is an impossible value, so it's used as a marker for "not found". Some languages instead return None or null.

Related