Linear Search Visualization: Step-by-Step Sequential Search

Linear (sequential) search compares the target with each item in turn from the start. It works on any list, sorted or not, but may have to look at every item.

About this visualization

Linear (sequential) search checks each item from the start until it finds the target or reaches the end. It works on any list, sorted or not, which makes it the simplest search — but it may have to look at every item.

How it works

  1. Start at the first item.
  2. Compare it with the target; if equal, stop — found.
  3. Otherwise move to the next item. If the end is reached, the target is not in the list.

Worked example

Search for 56 in [42, 7, 19, 88, 3, 56]. The algorithm compares 42, 7, 19, 88 and 3 (none match) and finds 56 at index 5 on the 6th comparison. If the target were 42 it would stop after 1 comparison (best case); if the target were absent it would make 6 (worst case).

Enter these numbers in the simulator above to see every step.

Complexity and properties

Best caseO(1)O(1)
Average caseO(n)O(n)
Worst caseO(n)O(n)
SpaceO(1)O(1)

Best case: the target is the first item.

When to use it

  • Searching small or unsorted lists.
  • Understanding best, average and worst cases.
  • Comparing with binary search to see when sorting first pays off.

Frequently asked questions

What is the time complexity of linear search?

O(1)O(1) in the best case, O(n)O(n) on average and in the worst case. Space is O(1)O(1).

Does linear search need a sorted list?

No. It works on any list.

When should I use linear search instead of binary search?

When the data is unsorted, very small, or you only search once so sorting would cost more than it saves.