Quarry School

Search, sort and cost — quick review

Insert one card into sorted order

In plain words

A library buys one new book.

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.

Step

  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.

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.

Trace a sort on repeated keys

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.

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.

Step

  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.

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.

Compare work as the collection doubles

In plain words

An algorithm's efficiency estimates the work it does, and it is stated in terms of the size of the input.

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.

Step

  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.

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.