Count comparisons in a linear search
Leans on
Quick review (2 minutes)
A box of ticket stubs sits in no particular order, and you need one particular stub.
A linear search checks the cards in order and stops at the first card that shows the target, or after the last card. It makes one comparison for each card it checks: one when the target is first, and as many as there are cards when its first match is the last card or it is not in the row.
Write the row and the target.
Compare the target with the first card and count 1.
If the card does not match and a card is left after it, move to the next card, compare and add 1 to the count. Repeat until a card matches or no card is left.
Announce the result: the position of the first matching card, or not found after the last card.
Check the count: it equals the position of the matching card, or the number of cards when nothing matched.
A linear search that stops early cannot say not found. If you check three cards of 7 3 9 4 for the target 4 and stop, the 4 on the last card is missed. A linear search says not found only after the last card.
When the target appears twice, the search stops at the first one. In the row 6 2 8 2 5, a search for 2 stops at position 2 after two comparisons and never reaches the second 2.
1 In plain words
A box of ticket stubs sits in no particular order, and you need one particular stub. With no order to use, the plan is to look at the stubs one after another until it turns up or no stub is left.
That plan is linear search, also called sequential search: check each item of a list, in order, until the item you want is found or every item has been checked.
In this unit the items are cards in a row of one or more cards, each showing a whole number, and the item you want is the target. Checking one card against the target is one comparison. The number of comparisons is this unit's measure of the work a procedure does, as the Field Guide counts the checks of its searches and the comparisons of its sorts.
The linear search of this lesson has four steps. Step 1: take the first card. Step 2: if it shows the target, announce found, with its position, and stop. Step 3: if no card is left after it, announce not found and stop. Step 4: take the next card and go to step 2. These steps are this lesson's way of writing the search the Field Guide describes.
Remember one sentence: a linear search makes one comparison for each card it reaches. To look it up, see essential knowledge AAP-2.O.5 in the AP Computer Science Principles Course and Exam Description.
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.2 Searching
- College Board, AP Computer Science Principles Course and Exam Description
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting
2 Picture it
A table of three linear searches of the row 7 3 9 4. The target 7 takes 1 comparison and is found at position 1. The target 9 takes 3 comparisons and is found at position 3. The target 5 takes 4 comparisons and is not found. The row for 5 is marked.
3 Why
The search stops as soon as a card matches, so the count depends on where the target is. A target on the first card costs one comparison. A target whose first match is the last card costs as many comparisons as there are cards.
A target that is not in the row costs the most: the search can say not found only after it has checked every card, because any unchecked card might be the target. So on a row of 4 cards the most a linear search can make is 4 comparisons, and on a row of 10,000 it is 10,000.
When the target is on exactly one card, equally likely to be any of them, a linear search looks at about half the cards on average. The Field Guide gives rough figures: about 5 of 10 items, and about 5,000 of 10,000.
4 The rule
A linear search checks the cards in order and stops at the first card that shows the target, or after the last card.
It makes one comparison for each card it checks: one when the target is first, and as many as there are cards when its first match is the last card or it is not in the row.
5 How
- Write the row and the target.
- Compare the target with the first card and count 1.
- If the card does not match and a card is left after it, move to the next card, compare and add 1 to the count. Repeat until a card matches or no card is left.
- Announce the result: the position of the first matching card, or not found after the last card.
- Check the count: it equals the position of the matching card, or the number of cards when nothing matched.
6 Trace
| Comparison | Card | Shows 9? | Explanation |
|---|---|---|---|
| 1 | 7 | no | The first card shows 7. Go on to the next card. |
| 2 | 3 | no | The second card shows 3. Go on. |
| 3 | 9 | yes | The third card shows 9. Announce found at position 3 and stop. The fourth card is never checked. |
7 The trap
A linear search that stops early cannot say not found. If you check three cards of 7 3 9 4 for the target 4 and stop, the 4 on the last card is missed. A linear search says not found only after the last card.
When the target appears twice, the search stops at the first one. In the row 6 2 8 2 5, a search for 2 stops at position 2 after two comparisons and never reaches the second 2.
Quick checks
Quick check 1 of 2
A linear search looks for the target 2 in the row 6 2 8 2 5. How many comparisons does it make, and at which position does it announce found?
Show the steps and the answer
It makes 2 comparisons and announces found at position 2.
Why: The first card shows 6, which does not match. The second card shows 2, which matches, so the search stops there and never reaches the second 2.
Quick check 2 of 2
A row holds 10 cards, and none of them shows the target. How many comparisons does a linear search make before it announces not found?
Show the steps and the answer
It makes 10 comparisons.
Why: Any unchecked card might show the target, so the search checks every card before it can announce not found: one comparison for each of the 10 cards.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- Computer Science Field Guide: Algorithms, 2.2 Searching. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 2 Dec 2026.
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 2 Dec 2026.
- Computer Science Field Guide: Algorithms, 2.3 Sorting. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 2 Dec 2026.