Edexcel International Advanced Level Computer Science, point 5.2.1
Definition and function
A linear (sequential) search finds an item in a list by checking each item in turn, from the start, until the item is found or the end of the list is reached.
Function: it finds the position of a target value (or reports that it isn't there). It is the simplest searching method, and it works on any list.
a. Linear search of an unsorted array
The data is in no particular order, so every item might need checking.
Algorithm:
- Start at the first item.
- Compare the item with the target.
- If they're equal, stop: the item is found at this position.
- Otherwise move to the next item.
- If you pass the last item, the target is not in the list.
def linear_search(items, target):
for i in range(len(items)):
if items[i] == target:
return i # found: return the position
return -1 # not found
Hand trace: find 23 in [8, 15, 4, 23, 16, 42]
| Step | Index | Item | Equal to 23? |
|---|---|---|---|
| 1 | 0 | 8 | No |
| 2 | 1 | 15 | No |
| 3 | 2 | 4 | No |
| 4 | 3 | 23 | Yes: found at index 3 |
It took 4 comparisons. Searching for 99 would check all 6 items and return -1.
b. Linear search of a sorted array
If the list is sorted (ascending), the search can stop early: as soon as it meets an item larger than the target, the target can't be further on.
def linear_search_sorted(items, target):
for i in range(len(items)):
if items[i] == target:
return i
if items[i] > target: # passed where it would be
return -1
return -1
Hand trace: find 12 in [3, 7, 10, 15, 20]
| Step | Index | Item | Comparison |
|---|---|---|---|
| 1 | 0 | 3 | 3 is not 12, and 3 < 12, so continue |
| 2 | 1 | 7 | 7 < 12, so continue |
| 3 | 2 | 10 | 10 < 12, so continue |
| 4 | 3 | 15 | 15 > 12, so stop: 12 isn't in the list |
Only 4 comparisons were needed, instead of 5. For a missing target in a sorted list, this saves time, but when the item is present the cost is the same as an unsorted search.
Efficiency
| Case | Comparisons for items |
|---|---|
| Best | 1 (the target is first) |
| Average (found) | About |
| Worst | (the target is last, or missing; sorted lists can stop early when missing) |
The time grows in proportion to : doubling the list doubles the work. It needs no extra memory.
Benefits
- Very simple to write and understand.
- Works on unsorted data, so no sorting needed.
- Works on any list structure, including linked lists.
- Good for small lists, or when you only search once.
Drawbacks
- Slow for large lists, since it may check every item.
- Doesn't take advantage of sorted data (unlike binary search, 5.2.2), apart from the early stop.
Worked example
Task: a program stores the names ["Zoe", "Amy", "Raj", "Li"] and searches for "Raj". Trace it and say how many comparisons are made. How many would a search for "Max" take?
| Step | Index | Name | Equal to "Raj"? |
|---|---|---|---|
| 1 | 0 | Zoe | No |
| 2 | 1 | Amy | No |
| 3 | 2 | Raj | Yes |
"Raj" is found at index 2 after 3 comparisons. "Max" isn't in the list, so all 4 names are checked (4 comparisons) and -1 is returned. The list is unsorted, so the early-stop method can't be used.
Exam tip: trace by writing each index, item and result. State when the search stops (found, or end of the list, or passed the position in a sorted list). For the drawback, say it may have to check every item, so it is slow for large lists.
Check yourself
How many comparisons does a linear search need in the worst case on 50 items?
How can a linear search of a sorted list stop early?
When it reaches an item larger than the target, because the target can't appear later in an ascending list.
Give one advantage of linear search over binary search
It works on unsorted data (and on linked lists), so no sorting is needed, and it's simpler.
What is the best case for a linear search?
The target is the first item, so only one comparison is needed.