AQA 7517, section 4.3.4.1
A linear search (also called a sequential search) finds an item in a list by checking each element in turn, from the start, until it either finds the item or reaches the end.
It is the simplest search algorithm, and it works on any list, whether or not it is sorted.
Algorithm
- Start at the first item.
- Compare the current item with the target.
- If they match, stop: the item has been found.
- Otherwise move on to the next item.
- If you reach the end without a match, the item is not in the list.
def linear_search(items, target):
for i in range(len(items)):
if items[i] == target:
return i # found: give back the position
return -1 # not found
data = [14, 7, 22, 3, 18, 9]
print(linear_search(data, 3)) # 3
print(linear_search(data, 50)) # -1
Trace
Searching for 18 in [14, 7, 22, 3, 18, 9]:
| Step | Index | Item | Equal to 18? |
|---|---|---|---|
| 1 | 0 | 14 | No |
| 2 | 1 | 7 | No |
| 3 | 2 | 22 | No |
| 4 | 3 | 3 | No |
| 5 | 4 | 18 | Yes, found at index 4 |
It took 5 comparisons.
Time complexity
The time taken depends on how many items must be checked.
| Case | When | Comparisons |
|---|---|---|
| Best | The target is the first item | 1 |
| Worst | The target is the last item or is missing | |
| Average | The target is somewhere in the middle | about |
If the list doubles in size, the worst case takes twice as many comparisons. The time grows in proportion to the number of items, so the time complexity is linear, written . (Big-O is explained in Order of complexity.)
Advantages and disadvantages
| Advantages | Disadvantages |
|---|---|
| Very simple to write and understand | Slow for large lists, since it may check every item |
| Works on an unsorted list | Each search starts again from the beginning |
| No set-up or sorting needed | |
| Fine for short lists, or for searching once |
Worked example
Task: a program searches for a name in an unsorted list of 1000 names. In the worst case, how many comparisons are made, and what about on average?
- Worst case: the name is last, or not in the list, so all 1000 names are compared.
- Average case (if the name is there): about 500 comparisons.
If the list were sorted, a binary search (4.3.4.2) would need only about 10 comparisons for the same list. But if the list is unsorted and only searched once, sorting it first would take longer than just using a linear search.
Exam tip: when you describe the complexity, link it to the data: "linear search is because in the worst case every one of the items has to be checked."
Check yourself
Does a linear search require the list to be sorted?
No. It works on any list.
What is the time complexity of a linear search?
, which is linear time.
How many comparisons does a linear search make in the best and worst cases?
Best case: 1 (the target is the first item). Worst case: (the target is last or isn't there).
Why might a linear search be a sensible choice despite being slow?
It is simple and works on unsorted data, so for a short list, or one that is only searched once, it avoids the cost of sorting first.