Compare work as the collection doubles
Quick review (2 minutes)
An algorithm's efficiency estimates the work it does, and it is stated in terms of the size of the input.
To compare the efficiency of two procedures, count a step, such as a comparison, as a function of the input size. When a row doubles, a linear search's worst case doubles, a binary search's grows by one check, and, from 4 cards on, selection sort's count grows a little more than four times.
Choose the measure of size, such as the number of cards, and the step to count, such as a comparison.
Write the worst-case count for one size.
Double the size and write the worst-case count again.
Compare the new count with the old one to see how the count grows.
Compare the growth of the procedures, not their counts on one small input.
Comparing one small input hides the difference. On 3 cards the two searches make at most 3 and at most 2 comparisons, but on 63 cards they make at most 63 and at most 6. Compare how the counts grow.
A best case can also mislead. A linear search whose target sits on the first card makes one comparison at any size; the worst case, a target absent from the row or first found on the last card, shows the cost of a big row.
1 In plain words
An algorithm's efficiency estimates the work it does, and it is stated in terms of the size of the input. One informal measure counts how many times a step runs. In this unit the size is the number of cards and the step counted is a comparison, in the worst case: the input that makes a procedure work hardest, as the Field Guide's searches count their most checks.
Different correct algorithms for the same problem can have different efficiencies. This lesson sees the difference by doubling the size of the input and comparing the counts.
Remember one sentence: count the comparisons, then double the cards and count again. To look it up, see essential knowledge AAP-4.A.3, AAP-4.A.5 and AAP-4.A.6 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of the worst-case comparisons of linear and binary search. A sorted row of 15 cards needs at most 15 comparisons for linear search and 4 for binary search. A row of 31 cards needs at most 31 and 5. A row of 63 cards needs at most 63 and 6. The row for 63 cards is marked.
3 Why
A linear search's worst case checks every card, so twice the cards means twice the comparisons. A binary search's first check, when it misses, removes the middle card and one side of the rest, so it leaves at most as many cards as before the doubling, and the worst case grows by one check. The Field Guide states the same: doubling the boxes doubles the most checks a linear search makes and adds one to the most a binary search makes.
Selection sort grows faster than both. Its count is 1 + 2 + ... up to one less than the number of cards: 28 comparisons for 8 cards, and 120 for 16. From 4 cards on, doubling the cards multiplies the count by a little more than four, closer to four as the rows grow.
A single small input can mislead. On 3 cards a linear search makes at most 3 comparisons and a binary search at most 2, which looks like little difference. The counts for growing inputs show which procedure keeps up.
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.2 Searching
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting
- College Board, AP Computer Science Principles Course and Exam Description
4 The rule
To compare the efficiency of two procedures, count a step, such as a comparison, as a function of the input size.
When a row doubles, a linear search's worst case doubles, a binary search's grows by one check, and, from 4 cards on, selection sort's count grows a little more than four times.
- College Board, AP Computer Science Principles Course and Exam Description
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.2 Searching
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting
5 How
- Choose the measure of size, such as the number of cards, and the step to count, such as a comparison.
- Write the worst-case count for one size.
- Double the size and write the worst-case count again.
- Compare the new count with the old one to see how the count grows.
- Compare the growth of the procedures, not their counts on one small input.
6 Trace
| Cards | Comparisons | Times the count before | Explanation |
|---|---|---|---|
| 4 | 6 | — | 3 + 2 + 1 is 6. |
| 8 | 28 | about 4.7 | 7 + 6 + 5 + 4 + 3 + 2 + 1 is 28, and 28 / 6 is about 4.7. |
| 16 | 120 | about 4.3 | The sum of 1 to 15 is 120, and 120 / 28 is about 4.3. |
| 32 | 496 | about 4.1 | The sum of 1 to 31 is 496, and 496 / 120 is about 4.1. |
7 The trap
Comparing one small input hides the difference. On 3 cards the two searches make at most 3 and at most 2 comparisons, but on 63 cards they make at most 63 and at most 6. Compare how the counts grow.
A best case can also mislead. A linear search whose target sits on the first card makes one comparison at any size; the worst case, a target absent from the row or first found on the last card, shows the cost of a big row.
Quick checks
Quick check 1 of 2
A linear search of a row of 40 cards makes at most 40 comparisons. At most how many does it make on a row of 80 cards?
Show the steps and the answer
At most 80 comparisons.
Why: The worst case checks every card, so doubling the cards doubles the worst-case count.
Quick check 2 of 2
A binary search makes at most 5 checks on a sorted row of 31 cards and at most 6 on 63 cards. At most how many does it make on a sorted row of 127 cards?
Show the steps and the answer
At most 7 checks.
Why: 127 cards are 63 doubled plus one. The first check reads the middle card, the 64th, and leaves at most 63 cards, which need at most 6 more checks.
Sources
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 2 Dec 2026.
- 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.
- 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.