Skip to content
BENIBIX Learn

Linear Search

Development🧮 Algorithms & Data StructuresSearch Algorithms

Also known as Sequential Search

Checking each item in turn until you find a match.

Think of it like

Think of it like looking for a sock. You open each drawer, one at a time.

Example

Jack looks for 9 in the list 4, 7, 1, 9. Linear search checks four numbers and finds it at position 3.

Why it matters

It is simple and always works. Knowing its cost tells you when to reach for something faster.

Definition

Linear search finds an item by checking each one in turn, from the start. It stops when it finds a match, or when it reaches the end. It works on any list, sorted or not. For a list of n items it can take up to n checks, so it is O(n). It is the right choice for short lists and lists you search only once.

Example code

python
>>> [4, 7, 1, 9].index(9)
3
index walks the list from the start until it finds 9.

Common mistake

Running a linear search inside a loop over the same big list. That quietly turns O(n) into O(n²).

Key takeaways

  1. 1
    One by one. From the start.
  2. 2
    Any list. Sorted or not.
  3. 3
    O(n). Slow on big lists.

Compare