16: Sorting

Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.


Suppose you have the array of numbers [3 2 5 4 1].

A. (1 point) In the style we showed in class, show the contents of the array during a selection sort. After each iteration through the outer for loop, rewrite the array, using a vertical bar (|) to show the division between the sorted and the unsorted part of the array. Show each iteration through the inner loop and indicate the elements that were swapped:

For example, on 5 3 7 1, indicating swapped elements with asterisks (you can underline or whatever is reasonably clear and easy):

| 5 3 7 1
*1* | 3 7 *5*
1 3 | 7 5
1 3 7 | 5
1 3 *5* *7* |

B. (1 point) As above, but with insertion sort. Again, an example (but you should do it on [3 2 5 4 1]):

| 5 3 7 1
5 | 3 7 1
*3* *5* | 7 1 
3 5 7 | 1
3 5 *1* *7* |
3 *1* *5* 7 |
3 *1* *5* 7 |
*1* *3* 5 7 |