Computer Science Foundations / Search, sort and cost
Search, sort and cost
All 5 lessons, in order.
The unit at a glance
- Count comparisons in a linear search
A box of ticket stubs sits in no particular order, and you need one particular stub.
- Keep a sorted interval for binary search
Someone thinks of a whole number from a known range and answers each guess with higher, lower or correct.
- Insert one card into sorted order
A library buys one new book.
- Trace a sort on repeated keys
Sorting a hand of cards by picking out the smallest, then the smallest of the rest, and so on, shows the idea of this lesson. Selection sort works that way.
- Compare work as the collection doubles
An algorithm's efficiency estimates the work it does, and it is stated in terms of the size of the input.
Rules (5)
- Count comparisons in a linear search
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.
- Keep a sorted interval for binary search
Binary search works only on a sorted row. Each check compares the target with the middle card of the interval: a match ends the search, and otherwise the interval keeps only the half that can still hold the target. When the interval is empty, the target is not in the row.
- Insert one card into sorted order
With this lesson's steps, a new card goes into a sorted row like this: compare it with the cards from the right-hand end and place it directly to the right of the first card that is not larger, or first in the row when every card is larger. The row stays sorted. Insertion sort, as the Field Guide describes it, inserts each card in turn into the sorted row so far.
- Trace a sort on repeated keys
Selection sort moves the smallest unsorted card to the sorted row, pass after pass, until no unsorted card is left, and finding the smallest of n unsorted cards takes n - 1 comparisons. With this lesson's rules, a card replaces the smallest so far only if its key is smaller, and the cards left unsorted keep their order; then the leftmost of equal smallest cards moves first, and equal keys keep their order.
- Compare work as the collection doubles
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.
Recipes (5)
- Count comparisons in a linear search
- 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.
- Keep a sorted interval for binary search
- Check that the row is sorted, smallest first. If it is not, binary search cannot be used.
- This lesson numbers positions from 1: write low = 1 and high = the number of cards, so the interval starts as the whole sorted row.
- If low is greater than high, the interval is empty: announce not found and stop.
- Work out middle = (low + high) / 2, rounded down: (1 + 2) / 2 is 1.5, which rounds down to 1. Read the card at that position. The search starts at the middle of the interval.
- Compare the target with that card and count 1 check: on a match, announce found at position middle and stop. For a smaller target write high = middle - 1, for a larger target write low = middle + 1, and go back to the third step.
- Insert one card into sorted order
- Write the sorted row and the new card.
- Compare the new card with the last card, and count 1.
- If the card compared is not larger, place the new card directly to its right: the insertion is done.
- If no card is left to its left, place the new card first: the insertion is done. Otherwise compare with the next card to the left, count 1 and go back to the third step.
- When the insertion is done, check that the new row is sorted and holds every card once.
- Trace a sort on repeated keys
- Write the unsorted row with each card told apart, and an empty sorted row.
- Mark the first unsorted card as the smallest so far.
- Compare the marked card with each later unsorted card in turn, counting each comparison. When a card's key is smaller than the marked card's key, move the mark to that card, and compare the cards after it with the newly marked card.
- Move the marked card to the end of the sorted row, and close up the unsorted row without changing its order.
- Repeat until no unsorted card is left, then add up the comparisons.
- Compare work as the collection doubles
- 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.
Traps (5)
- Count comparisons in a linear search
A linear search that stops early cannot say not found. If you check three cards of
7 3 9 4for 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. - Keep a sorted interval for binary search
Binary search on a row that is not sorted can miss a target that is there. In the row
9 2 7 4 5, a search for 4 reads the middle card 7, keeps positions 1 to 2 because 4 is smaller, reads the 9, and ends with not found, though the 4 sits at position 4. - Insert one card into sorted order
The case with no stopping card needs its own step. Inserting 2 into
4 9 13passes 13, 9 and 4, all larger, and reaches the left-hand end with no card that is not larger. The step for that case places the new card first, giving2 4 9 13.A second slip is placing the new card to the left of the stopping card. Inserting 12 next to 11 that way gives
3 8 12 11 15 20, which is not sorted. The new card goes to the right of the first card that is not larger. - Trace a sort on repeated keys
One word changes the order of equal keys. If the mark switched to a card that is smaller or equal, pass 3 would move the blue 4 first, and the sorted row would end blue 4, red 4. Both rows are sorted by key, but only one keeps the start order of the fours. Write the test you mean.
A second slip is comparing against cards already sorted. Each pass compares only unsorted cards, which is why the count falls by one each pass.
- Compare work as the collection doubles
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.
Lesson 1 of 5
Count comparisons in a linear search
Open this lessonLeans on 1Comes back in 0
Leans on
Comes back in
No lesson links recorded.
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.
Lesson 2 of 5
Keep a sorted interval for binary search
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
Someone thinks of a whole number from a known range and answers each guess with higher, lower or correct. A guess in the middle of the numbers still possible either ends the game or rules out about half of them.
Binary search finds a target in a row the same way. It starts at the middle of a sorted row, and each comparison that misses rules out at least half of the cards that are left, until the target is found or no card is left. The row must be in sorted order, smallest first in this unit.
The cards still in play form the interval: every card from a low position to a high position. The binary search of this lesson has five steps. Step 1: set low to 1 and high to the number of cards. Step 2: if low is greater than high, announce not found and stop. Step 3: set middle to the position halfway between low and high, (low + high) / 2, rounded down when it is not a whole number. Step 4: compare the target with the card at middle. If it matches, announce found and stop. If the target is smaller, set high to middle - 1. If the target is larger, set low to middle + 1. Step 5: go to step 2. These steps are this lesson's way of writing the search the Field Guide and the AP course describe.
Remember one sentence: binary search keeps only the half that can still hold the target. To look it up, see essential knowledge AAP-2.P.1 and AAP-2.P.2 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of a binary search for 23 in the sorted row 2 5 8 12 16 23 38 41 56 72 81 85 90 94 99. Check 1 reads 41, the middle card of positions 1 to 15. Check 2 reads 12, the middle card of positions 1 to 7. Check 3 reads 23, the target, the middle card of positions 5 to 7. The last row is marked.
3 Why
Sorting is what lets the search drop a half: the dropped positions cannot hold the target. In a row sorted smallest first, every card to the right of the middle is at least as large as the middle card. If the target is smaller than the middle card, it cannot be to the right, so high moves to middle - 1. If the target is larger, it cannot be to the left, so low moves to middle + 1.
A check that misses removes the middle card and the half that cannot hold the target; a match ends the search. In the sorted row of 15 cards used here, the interval goes from 15 cards to at most 7, then 3, then 1, so any search ends after at most 4 checks. The Field Guide gives the same kind of count: at most 10 checks for 1,000 boxes.
On data that is not sorted, a miss still drops at least half of the interval, the middle card among them, and the dropped cards can include the target. Binary search is often more efficient than linear search on sorted data, and it cannot be used on data that is not sorted.
4 The rule
Binary search works only on a sorted row.
Each check compares the target with the middle card of the interval: a match ends the search, and otherwise the interval keeps only the half that can still hold the target. When the interval is empty, the target is not in the row.
5 How
- Check that the row is sorted, smallest first. If it is not, binary search cannot be used.
- This lesson numbers positions from 1: write low = 1 and high = the number of cards, so the interval starts as the whole sorted row.
- If low is greater than high, the interval is empty: announce not found and stop.
- Work out middle = (low + high) / 2, rounded down: (1 + 2) / 2 is 1.5, which rounds down to 1. Read the card at that position. The search starts at the middle of the interval.
- Compare the target with that card and count 1 check: on a match, announce found at position middle and stop. For a smaller target write high = middle - 1, for a larger target write low = middle + 1, and go back to the third step.
6 Trace
| Check | Interval | Middle card | Then | Explanation |
|---|---|---|---|---|
| 1 | 1 to 15 | 41 at position 8 | 50 > 41: low = 9 | (1 + 15) / 2 is 8. The target is larger, so the left half and the middle go. |
| 2 | 9 to 15 | 85 at position 12 | 50 < 85: high = 11 | (9 + 15) / 2 is 12. The target is smaller, so the right half and the middle go. |
| 3 | 9 to 11 | 72 at position 10 | 50 < 72: high = 9 | (9 + 11) / 2 is 10. The target is smaller again. |
| 4 | 9 to 9 | 56 at position 9 | 50 < 56: high = 8 | One card is left, and it is not the target. |
| — | 9 to 8 | — | not found | Low, 9, is greater than high, 8: the interval is empty, so 50 is not in the row. |
7 The trap
Binary search on a row that is not sorted can miss a target that is there. In the row 9 2 7 4 5, a search for 4 reads the middle card 7, keeps positions 1 to 2 because 4 is smaller, reads the 9, and ends with not found, though the 4 sits at position 4.
Quick checks
Quick check 1 of 2
A binary search looks for 90 in the sorted row 2 5 8 12 16 23 38 41 56 72 81 85 90 94 99. Which middle cards does it read, and how many checks does it make?
Show the steps and the answer
It reads 41, 85, 94 and then 90, so it makes 4 checks and finds 90 at position 13.
Why: Positions 1 to 15 give middle 8, the 41, and 90 is larger, so low = 9. Positions 9 to 15 give middle 12, the 85, so low = 13. Positions 13 to 15 give middle 14, the 94, and 90 is smaller, so high = 13. Position 13 holds 90.
Quick check 2 of 2
The row 9 2 7 4 5 is not sorted. A binary search for 4 follows the steps anyway. Which cards does it read, and what does it announce?
Show the steps and the answer
It reads 7 and then 9, and it announces not found, though 4 is at position 4.
Why: Middle 3 holds 7, and 4 is smaller, so high = 2. Middle 1 holds 9, and 4 is smaller, so high = 0, below low. The 4 was in the dropped positions 4 to 5: in a row that is not sorted, a card smaller than the middle card can lie to its right.
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.
Lesson 3 of 5
Insert one card into sorted order
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
A library buys one new book. Nobody re-sorts the whole shelf: the new book goes into its place, and every other book stays in its order.
Insertion puts one new card into a sorted row of one or more cards at its correct position. The insertion of this lesson compares from the right-hand end. Step 1: compare the new card with the last card of the row. Step 2: if the card compared is not larger than the new card, place the new card directly to its right and stop. Step 3: if no card is left to the left of the card compared, place the new card first and stop. Step 4: compare the new card with the next card to the left and go to step 2. These steps are this lesson's own way of finding the correct position the Field Guide describes.
Insertion sort builds a whole sorted row this way: it starts with one card and inserts each remaining card, one at a time, into the sorted row so far.
Remember one sentence: compare from the end and stop at the first card that is not larger. To look up insertion sort, see section 2.3.3 of the Computer Science Field Guide's algorithms chapter.
2 Picture it
A table of three comparisons that insert 12 into the sorted row 3 8 11 15 20. The 20 is larger than 12, and so is the 15. The 11 is not larger, so the comparisons stop there and 12 goes directly to the right of 11. The last row is marked.
3 Why
Because the row is sorted, every card to the left of a card that is not larger is not larger either. So the first card that is not larger, counting from the right, marks the place: the new card goes directly to its right, and the larger cards it passed stay to the right of it.
The cost depends on where the new card belongs. A new card that is larger than every card takes one comparison, with the last card. A new card smaller than every card takes one comparison for each card in the row. In the example, 12 belongs between 11 and 15 and takes three comparisons.
A card equal to the new one is not larger, so the comparisons stop at it and the new card goes to its right. Equal cards end up in the order in which they were inserted.
4 The rule
With this lesson's steps, a new card goes into a sorted row like this: compare it with the cards from the right-hand end and place it directly to the right of the first card that is not larger, or first in the row when every card is larger.
The row stays sorted. Insertion sort, as the Field Guide describes it, inserts each card in turn into the sorted row so far.
5 How
- Write the sorted row and the new card.
- Compare the new card with the last card, and count 1.
- If the card compared is not larger, place the new card directly to its right: the insertion is done.
- If no card is left to its left, place the new card first: the insertion is done. Otherwise compare with the next card to the left, count 1 and go back to the third step.
- When the insertion is done, check that the new row is sorted and holds every card once.
6 Trace
| Comparison | Card compared | Row | Explanation |
|---|---|---|---|
| 1 | 20 | 3 8 11 15 20 | 20 is larger than 12. Move one card to the left. |
| 2 | 15 | 3 8 11 15 20 | 15 is larger than 12. Move one card to the left. |
| 3 | 11 | 3 8 11 12 15 20 | 11 is not larger than 12, so 12 goes directly to the right of 11. The row is sorted. |
7 The trap
The case with no stopping card needs its own step. Inserting 2 into 4 9 13 passes 13, 9 and 4, all larger, and reaches the left-hand end with no card that is not larger. The step for that case places the new card first, giving 2 4 9 13.
A second slip is placing the new card to the left of the stopping card. Inserting 12 next to 11 that way gives 3 8 12 11 15 20, which is not sorted. The new card goes to the right of the first card that is not larger.
Quick checks
Quick check 1 of 2
Insert the card 2 into the sorted row 4 9 13 with this lesson's steps. How many comparisons are made, and what is the new row?
Show the steps and the answer
3 comparisons are made, and the new row is 2 4 9 13.
Why: By this lesson's steps, 13, 9 and 4 are compared from the right, and all are larger than 2, one comparison each. No card is left, so the 2 goes first.
Quick check 2 of 2
Insert a second 9 into the sorted row 4 9 13 with this lesson's steps. How many comparisons are made, and on which side of the old 9 does the new 9 go?
Show the steps and the answer
2 comparisons are made, and the new 9 goes to the right of the old 9.
Why: By this lesson's steps, 13 is compared first and is larger than 9. The old 9 is not larger than the new 9, so the comparisons stop there and the new card goes directly to its right: 4 9 9 13.
Sources
- 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.
Lesson 4 of 5
Trace a sort on repeated keys
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
Sorting a hand of cards by picking out the smallest, then the smallest of the rest, and so on, shows the idea of this lesson.
Selection sort works that way. It finds the smallest card in the unsorted row and moves it to the end of a sorted row, then repeats with the cards that are left until none is left.
The number on a card is its key, the value the sort puts in order. Two cards can share a key. In this lesson's invented row, a red 4 and a blue 4 both have the key 4, and the color shows which is which.
The selection sort of this lesson finds the smallest card in a fixed way. Take the first unsorted card as the smallest so far. Compare it with each later unsorted card in turn, and a card replaces it only when that card's key is smaller. Then move the smallest so far to the sorted row; the other unsorted cards keep their order. These are this lesson's own rules for carrying out the selection sort the Field Guide describes.
Remember one sentence: select the smallest, move it, repeat. To look up selection sort, see section 2.3.2 of the Computer Science Field Guide's algorithms chapter.
2 Picture it
A table of the four passes of selection sort on the row red 4, 2, blue 4, 1. Pass 1 moves the 1 after 3 comparisons. Pass 2 moves the 2 after 2 comparisons. Pass 3 moves the red 4 after 1 comparison. Pass 4 moves the blue 4 with no comparison. The row for pass 3 is marked.
3 Why
Finding the smallest of a group takes one comparison for each card after the first. The Field Guide gives the pattern: finding the lightest of 8 boxes takes 7 comparisons, and finding the next lightest of the remaining boxes takes 6. On 4 cards the passes take 3, 2, 1 and 0 comparisons, 6 in all.
The replacement test decides what happens to equal keys. On pass 3 the unsorted cards are the red 4 and then the blue 4. The blue 4 is not smaller than the red 4, so the red 4 stays the smallest so far and moves first. With this test the leftmost of several equal smallest cards is always the one moved, so equal keys keep their order from the start row.
4 The rule
Selection sort moves the smallest unsorted card to the sorted row, pass after pass, until no unsorted card is left, and finding the smallest of n unsorted cards takes n - 1 comparisons.
With this lesson's rules, a card replaces the smallest so far only if its key is smaller, and the cards left unsorted keep their order; then the leftmost of equal smallest cards moves first, and equal keys keep their order.
5 How
- Write the unsorted row with each card told apart, and an empty sorted row.
- Mark the first unsorted card as the smallest so far.
- Compare the marked card with each later unsorted card in turn, counting each comparison. When a card's key is smaller than the marked card's key, move the mark to that card, and compare the cards after it with the newly marked card.
- Move the marked card to the end of the sorted row, and close up the unsorted row without changing its order.
- Repeat until no unsorted card is left, then add up the comparisons.
6 Trace
| Pass | Unsorted row | Sorted row | Explanation |
|---|---|---|---|
| start | red 4, 2, blue 4, 1 | — | Nothing is sorted yet. |
| 1 | red 4, 2, blue 4 | 1 | The mark moves from red 4 to 2, then to 1: 3 comparisons. The 1 moves. |
| 2 | red 4, blue 4 | 1, 2 | The mark moves from red 4 to 2. Blue 4 is then compared with 2 and is not smaller: 2 comparisons. The 2 moves. |
| 3 | blue 4 | 1, 2, red 4 | Blue 4 is not smaller than red 4, so the mark stays on red 4: 1 comparison. Red 4 moves. |
| 4 | — | 1, 2, red 4, blue 4 | One card is left and needs no comparison. Blue 4 moves. 6 comparisons in all. |
7 The trap
One word changes the order of equal keys. If the mark switched to a card that is smaller or equal, pass 3 would move the blue 4 first, and the sorted row would end blue 4, red 4. Both rows are sorted by key, but only one keeps the start order of the fours. Write the test you mean.
A second slip is comparing against cards already sorted. Each pass compares only unsorted cards, which is why the count falls by one each pass.
Quick checks
Quick check 1 of 2
Selection sort with this lesson's steps puts a row of 5 cards in order. How many comparisons does it make in all?
Show the steps and the answer
It makes 10 comparisons.
Why: Finding the smallest of 5, 4, 3, 2 and 1 cards takes 4, 3, 2, 1 and 0 comparisons, and 4 + 3 + 2 + 1 is 10.
Quick check 2 of 2
Suppose the mark switched to a card whose key is smaller or equal. Which four moves first when this changed selection sort sorts the row red 4, 2, blue 4, 1?
Show the steps and the answer
The blue 4 moves first, and the sorted row is 1, 2, blue 4, red 4.
Why: With this lesson's rules and that one change, the first two passes move the 1 and the 2, so on pass 3 the unsorted row is red 4, blue 4. The blue 4 is equal to the red 4, so with the changed test the mark switches to it, and it moves first.
Sources
- 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.
Lesson 5 of 5
Compare work as the collection doubles
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
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.