CMPS 134 Fall 2019
Quiz #2 Sample Solutions

1. Tracing Selection Sort

The premise of the problem was that four iterations of the "main" loop of SelectionSort had already taken place, leaving the array being sorted in the following state:

     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  | -2 |  0 |  4 |  7 | 19 | 26 | 10 | 35 |  8 | 13 | 22 |
  +----+----+----+----+----+----+----+----+----+----+----+

The student was asked to show the state of the array after each of the next three loop iterations. The manner in which Selection Sort works is that the k-th iteration swaps the value in location k-1 with the value in whatever location, among those numbered k-1 or greater, contains the smallest value. It follows that, after the k-th iteration, the k smallest values in the array occupy locations [0..k), and they are in ascending order.

Solution:

After the 5th iteration (during which values in locations 4 and 8 are swapped):

     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  | -2 |  0 |  4 |  7 |  8 | 26 | 10 | 35 | 19 | 13 | 22 |
  +----+----+----+----+----+----+----+----+----+----+----+

After the 6th iteration (during which values in locations 5 and 6 are swapped):

     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  | -2 |  0 |  4 |  7 |  8 | 10 | 26 | 35 | 19 | 13 | 22 |
  +----+----+----+----+----+----+----+----+----+----+----+

After the 7th iteration (during which values in locations 6 and 9 are swapped):

     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  | -2 |  0 |  4 |  7 |  8 | 10 | 13 | 35 | 19 | 26 | 22 |
  +----+----+----+----+----+----+----+----+----+----+----+

2. Tracing Insertion Sort

The premise of the problem was that three iterations of the "main" loop of InsertionSort had already taken place, leaving the array being sorted in the following state:

     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  |  5 |  7 | 14 | 22 |  6 |  2 | 35 | 15 |  8 | 22 | 13 |
  +----+----+----+----+----+----+----+----+----+----+----+

The student was asked to show the state of the array after each of the next three loop iterations. The manner in which Insertion Sort works is that the k-th iteration places the value in location k into its "proper place" among those preceding it. Thus, after the k-th iteration, the values occupying locations [0..k] are precisely those that were originally in that segment, but now they are in ascending order. Meanwhile, the values in locations k+1 and above remain exactly where they started.

Solution: After the 4th iteration (during which the value in location 4 is "inserted" into its proper place among values in locations [0..4]):

     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  |  5 |  6 |  7 | 14 | 22 |  2 | 35 | 15 |  8 | 22 | 13 |
  +----+----+----+----+----+----+----+----+----+----+----+
After the 5th iteration (during which the value in location 5 is "inserted" into its proper place among values in locations [0..5]):
     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  |  2 |  5 |  6 |  7 | 14 | 22 | 35 | 15 |  8 | 22 | 13 |
  +----+----+----+----+----+----+----+----+----+----+----+
After the 6th iteration (during which the value in location 6 is "inserted" into its proper place among values in locations [0..6]):
     0    1    2    3    4    5    6    7    8    9   10
  +----+----+----+----+----+----+----+----+----+----+----+
  |  2 |  5 |  6 |  7 | 14 | 22 | 35 | 15 |  8 | 22 | 13 |
  +----+----+----+----+----+----+----+----+----+----+----+