Article
Linear search
You look for your name on a printed class list. You run your finger down the page: first name, second name, third name… until you find yours or reach the bottom. You just did a linear search.
Check one by one
A linear search (also called a sequential search) looks at the items of a list one at a time, in order, starting with the first. It stops when it finds the target, or when it has checked every item.
It works on any list. The list does not have to be in any order. That is its big strength.
In Unit 3 you wrote a version with FOR EACH that returns true or false. This version says where the target is. It gives the position (counting from 1), or 0 if the target is not there. 0 can never be a real AP position, so it is a safe way to say "not found".
PROCEDURE linearSearch(aList, target)
{
i ← 1
REPEAT UNTIL(i > LENGTH(aList))
{
IF(aList[i] = target)
{
RETURN(i)
}
i ← i + 1
}
RETURN(0)
}Here is the same idea as a flowchart. Follow the arrows:
Linear search
Trace it. The list is ["Mia", "Omar", "Ana", "Li", "Zoe"] and the target is "Li":
| Check | i | aList[i] | aList[i] = "Li"? |
|---|---|---|---|
| 1 | 1 | Mia | false |
| 2 | 2 | Omar | false |
| 3 | 3 | Ana | false |
| 4 | 4 | Li | true, so RETURN(4) |
The procedure returns 4 after 4 checks. With the target "Sam", it checks all 5 names, i becomes 6, the loop ends, and it returns 0. Run the Python version and change the target:
Python counts from 0, so the code returns i + 1 to give the AP position. It also works for numbers: change the list to [12, 7, 30, 5] and the target to 30.
Check your understanding
A linear search looks for 5 in the list [12, 7, 30, 5, 9]. How many items does it check?
How many checks?
The number of checks depends on where the target is. Take a list of n items:
| Situation | Items checked |
|---|---|
| Target is the first item (best case) | 1 |
| Target is in the middle | about n / 2 |
| Target is the last item | n |
| Target is not in the list | n (worst case) |
The worst case is n checks: the algorithm must look at every item before it can say "not there". So the steps grow in step with n. For 1,000 items, at most 1,000 checks. For a million items, at most 1,000,000.
If the target appears twice, the search stops at the first one.
When to use it
- The list is not sorted: linear search is the only choice of the two searches in this lesson.
- The list is short, or you search it only once: the simple method is fine.
- The list is huge and you search it many times: a faster method is worth it (the next article).
Check your understanding
Which statement about linear search is true?
Common mistakes
- Thinking the list must be sorted. Linear search works on any list.
- Returning "not found" too early.
RETURN(0)belongs after the loop. Inside the loop, the search gives up after one item. - Counting n − 1 checks for a missing target. It needs all n.
- Using the Python index as the position. Python index 3 is AP position 4.
- Stopping at the last match. The search stops at the first match.
On the exam
Questions give a list and a target and ask "How many items are checked?" or "What is returned?". Count from the first item, in order, and stop at the first match. Another common one: "Which statement about linear search is true?" Remember: any list, one by one, worst case is every item.
Key words
Linear search (sequential search): checking a list one item at a time, in order, until the target is found or the list ends.
Target: the value being searched for.