datarekha

Linear Search

The simplest search there is — look at each item in turn until you find what you want. Slow when the data is huge, but often exactly right.

6 min read Beginner Data Structures & Algorithms Lesson 5 of 32

What you'll learn

  • How linear search checks each element in order and reports the position, or that the value is absent, in O(n) time
  • Why it needs no setup at all — unsorted, messy, one-off data is no problem
  • That Python's `in` and `.index()` are linear searches underneath
  • When a plain scan genuinely beats a cleverer algorithm

Before you start

Suppose someone hands you a shuffled stack of exam papers and asks you to pull out Meera’s.

There is no order to exploit — the papers could be in any arrangement — so you do the only sensible thing. You look at the top paper; if it is not hers, you set it aside and look at the next; and you keep going until her name turns up or the stack runs out. That is the whole of linear search, and its plainness is exactly the point.

Looking at each item in turn

Let us make it concrete. Here are eight temperature readings, in no particular order:

[22, 18, 35, 12, 29, 41, 7, 33]

To find 29, we begin at the left and compare each reading with our target, one at a time:

22?  no     18?  no     35?  no     12?  no     29?  yes — found, at index 4

Five looks. And to search for a value that is not there at all — say 99 — there is no shortcut: we have to check all eight before we can be sure it is missing. That “check everything to be certain” is the price of having no order to lean on.

scan left to right until a match appears →221835122941733✓ match01234567
Five looks to find 29. A value that is not in the list costs all eight before you can call it absent.

In code, the loop is as short as the idea:

def linear_search(arr, target):
    for i, value in enumerate(arr):
        if value == target:
            return i         # found it — hand back the position
    return -1                # fell off the end — not here

temps = [22, 18, 35, 12, 29, 41, 7, 33]
print(linear_search(temps, 29))   # the index of 29
print(linear_search(temps, 99))   # 99 is not present

This prints:

4
-1

When the target is the last element, or missing entirely, we make n comparisons for n items. That is the worst case, and it is O(n): double the data, double the work.

You have already used linear search without writing it. Python’s in and .index() run this very loop on your behalf:

29 in temps        # the same scan, stopping at the first match
temps.index(29)    # the same scan, raising ValueError if absent

Writing it out by hand simply makes the comparisons visible.

When a plain scan is the right call

Binary search — the next lesson — is faster, O(log n), but it demands a sorted array, and sorting costs O(n log n). That trade-off is what decides which tool fits.

Reach for linear search when:

  • The data is unsorted and you need one lookup. Sorting a ten-item list just to binary-search it once is pure overhead.
  • n is small. Below a few hundred items, the gap between O(n) and O(log n) is a handful of nanoseconds; the simpler code wins.
  • It is a one-off. Search the same data thousands of times and an index pays off; search it once and a scan is fine.
  • The items only support equality, not ordering. You cannot binary-search objects you cannot sort.

Practice

Quick check

0/2
Q1A list has 1,000 elements in no particular order. In the absolute worst case, how many comparisons does linear search make?
Q2You receive a 20-row CSV once a day and need to find one value in it. What is the most sensible approach?

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Practice this in an interview

All questions

Related lessons

Explore further

Skip to content