Quarry School

Computer Science Foundations / Search, sort and cost

Search, sort and cost

Subject: Computing

All 5 lessons, in order.

The unit at a glance

  1. Count comparisons in a linear search

    A box of ticket stubs sits in no particular order, and you need one particular stub.

  2. 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.

  3. Insert one card into sorted order

    A library buys one new book.

  4. 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.

  5. 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
    1. Write the row and the target.
    2. Compare the target with the first card and count 1.
    3. 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.
    4. Announce the result: the position of the first matching card, or not found after the last card.
    5. 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
    1. Check that the row is sorted, smallest first. If it is not, binary search cannot be used.
    2. This lesson numbers positions from 1: write low = 1 and high = the number of cards, so the interval starts as the whole sorted row.
    3. If low is greater than high, the interval is empty: announce not found and stop.
    4. 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.
    5. 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
    1. Write the sorted row and the new card.
    2. Compare the new card with the last card, and count 1.
    3. If the card compared is not larger, place the new card directly to its right: the insertion is done.
    4. 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.
    5. When the insertion is done, check that the new row is sorted and holds every card once.
  • Trace a sort on repeated keys
    1. Write the unsorted row with each card told apart, and an empty sorted row.
    2. Mark the first unsorted card as the smallest so far.
    3. 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.
    4. Move the marked card to the end of the sorted row, and close up the unsorted row without changing its order.
    5. Repeat until no unsorted card is left, then add up the comparisons.
  • Compare work as the collection doubles
    1. Choose the measure of size, such as the number of cards, and the step to count, such as a comparison.
    2. Write the worst-case count for one size.
    3. Double the size and write the worst-case count again.
    4. Compare the new count with the old one to see how the count grows.
    5. 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 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.

  • 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 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.

  • 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 3 of 5

Insert one card into sorted order

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

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

Compare 12 withLarger than 12?20yes15yes11no: stop

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.

Inserting 12 into the sorted row 3 8 11 15 20

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

  1. Write the sorted row and the new card.
  2. Compare the new card with the last card, and count 1.
  3. If the card compared is not larger, place the new card directly to its right: the insertion is done.
  4. 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.
  5. When the insertion is done, check that the new row is sorted and holds every card once.

6 Trace

Trace the insertion of 12 into the row 3 8 11 15 20 with this lesson's steps
ComparisonCard comparedRowExplanation
1203 8 11 15 2020 is larger than 12. Move one card to the left.
2153 8 11 15 2015 is larger than 12. Move one card to the left.
3113 8 11 12 15 2011 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
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.

Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting

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
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.

Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting

Sources

  1. 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

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

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

PassCard movedComparisons1132223red 414blue 40

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.

The four passes of selection sort on red 4, 2, blue 4, 1

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

  1. Write the unsorted row with each card told apart, and an empty sorted row.
  2. Mark the first unsorted card as the smallest so far.
  3. 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.
  4. Move the marked card to the end of the sorted row, and close up the unsorted row without changing its order.
  5. Repeat until no unsorted card is left, then add up the comparisons.

6 Trace

Trace selection sort on the row red 4, 2, blue 4, 1 with this lesson's rules
PassUnsorted rowSorted rowExplanation
startred 4, 2, blue 4, 1—Nothing is sorted yet.
1red 4, 2, blue 41The mark moves from red 4 to 2, then to 1: 3 comparisons. The 1 moves.
2red 4, blue 41, 2The mark moves from red 4 to 2. Blue 4 is then compared with 2 and is not smaller: 2 comparisons. The 2 moves.
3blue 41, 2, red 4Blue 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 4One 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
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.

Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting

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
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.

Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting

Sources

  1. 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

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

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

CardsLinear worstBinary worst151543131563636

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.

Worst-case comparisons of the two searches as the row grows

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.

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.

5 How

  1. Choose the measure of size, such as the number of cards, and the step to count, such as a comparison.
  2. Write the worst-case count for one size.
  3. Double the size and write the worst-case count again.
  4. Compare the new count with the old one to see how the count grows.
  5. Compare the growth of the procedures, not their counts on one small input.

6 Trace

Selection sort's comparisons as the row doubles
CardsComparisonsTimes the count beforeExplanation
46—3 + 2 + 1 is 6.
828about 4.77 + 6 + 5 + 4 + 3 + 2 + 1 is 28, and 28 / 6 is about 4.7.
16120about 4.3The sum of 1 to 15 is 120, and 120 / 28 is about 4.3.
32496about 4.1The 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
Answer

At most 80 comparisons.

Why: The worst case checks every card, so doubling the cards doubles the worst-case count.

Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.2 Searching

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
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.

Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.2 Searching

Sources

  1. AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 2 Dec 2026.
  2. 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.
  3. 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.