Learn › Searching

Linear search

The baseline: check every element until you find the target. · 4 min read

The most direct way to find a value is to look at every element until you spot it (or run out). No assumptions about the data, no setup — just a single pass.

def linear_search(a, target):
    for i in range(len(a)):
        if a[i] == target:
            return i
    return -1

Interactive visualization: Scanning one item at a time — step through it frame by frame on this page.

Previous: Binary, operators & bit tricks · Next: Binary search

More in Searching