Week 13: Exercises

1. Unstable Selection Sort

Write a function that performs a selection sort on an array, using an arbitrary function to map each array element to a sorting key. Then demonstrate that the sort is not stable by finding an input array and key function such that the output is different than it would be with a stable sort.

2. Unstable Quicksort

Repeat the preceding exercise to show that quicksort with Hoare partitioning is not stable.

3. Radix Digits

Modify the radix sort function from the lecture so that it takes a base as a parameter rather than assuming that it is 10. Also modify the function so that the caller does not need to specify the maximum number of digits k; the function should determine that automatically from the numbers in the input array.

4. Quartic Sort

Given n integers in the range 0 ≤ i ≤ n4 – 1, show how it is possible to sort them in O(n) time using O(n) memory.