Quarry School

Search, sort and cost: crash course

about 5 min to listen5 lessons0 questions to tryUnit 4 of 5

Short view. Each lesson's rule, its steps and its trap. Switch to Full for a worked example and a question to try in every lesson.Full view. Each lesson's rule, its steps, one worked example with Watch it solved, the trap, and one question to try.

Starts here no skill from another unit needed first

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.

How

  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.

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.

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.

How

  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.

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.

3 Insert one card into sorted order

about 1 minOpen the full lessonOnly this lesson

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.

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.

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.

4 Trace a sort on repeated keys

about 1 minOpen the full lessonOnly this lesson

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.

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.

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.

5 Compare work as the collection doubles

about 1 minOpen the full lessonOnly this lesson

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.

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.

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.