Skip to content

Linear Search

Lesson 1 of 8 · 3 min

Linear Search

Step 1 of 7

Linear search needs no order and no setup — it just looks at everything until it finds gray.

The Idea

Check items one at a time until you find the target or run out of items. It needs no ordering and no preparation at all. The worst case is O(n), and for small or unsorted data that is genuinely the right choice.

Real-World Example

Waiting at the baggage carousel, you inspect each bag as it rolls past. The bags arrive in no useful order, so there is nothing smarter to do than look at every one until yours appears.

The Code

Your turn

Put the steps in the right order.

  1. Return -1 because nothing matched
  2. Compare the current item to the target
  3. Start at index 0
  4. Move to the next index when there is no match

Mini quiz

1 / 3

Worst-case comparisons for a linear search over n items?