Skip to content
← Back to algorithms

Step 0 / 0 · Comparisons: 0 · Swaps: 0

How this algorithm works

How it works

  1. Start at the first element of the list.
  2. Compare the current element to the target value.
  3. If they match, the search succeeds — return the current index.
  4. Otherwise, move to the next element and repeat the comparison.

Key concepts

  • No ordering requirement (works on unsorted data)
  • Sequential, exhaustive scan in the worst case
  • Best case O(1) when the target is the first element
  • Foundation for understanding why sorted-data algorithms like binary search exist

When to use it

When the data is unsorted, small, or searched only once — sorting first just to binary-search would cost more than a single linear pass.

Did you know

Despite its simplicity, linear search is provably optimal for unsorted data: no comparison-based algorithm can guarantee better than O(n) without first sorting or indexing.