Sorting I (incl. Selection, Heapsort) Guide
Author: Josh Hug


Inversions. The number of pairs of elements in a sequence that are out of order. An array with no inversions is ordered.

Selection sort. One way to sort is by selection. The naive implementation of such an algorithm is in place.

Naive Heapsort. A variant of selection sort is to use a heap based PQ to sort the items. To do this, insert all items into a MaxPQ and then remove them one by one. The first such item removed is placed at the end of the array, the next item right before the end, and so forth until that last item deleted is placed in position 0 of the array. Each insertion and deletion takes O(log N) time, and there are N insertions and deletions, resulting in a O(N log N) runtime. With some more work, we can show that heapsort is θ(N log N) in the worst case. This naive version of heapsort uses θ(N) for the PQ. Creation of the MaxPQ requires O(N) memory. It is also possible to use a MinPQ instead.

In place heapsort. When sorting an array, we can avoid the θ(N) memory cost by treating the array itself as a heap. To do this, we first heapify the array using bottom-up heap construction (taking θ(N) time). We then repeatedly delete the max item, swapping it with the last item in the heap. Over time, the heap shrinks from N items to 0 items, and the sorted list from 0 items to N items. The resulting version is also θ(N log N).

Mergesort. We can sort by merging, as discussed in an earlier lecture. Mergesort is θ(N log N) and uses θ(N) memory.

B level

  1. Give a base case input for heapsort.
  2. Give a best and worst case input for selection sort.
  3. Give a best and worst case input for mergesort.
  4. What is the max number of exchanges involving a particular item during selection sort? What is the average number of exchanges?
  5. Problem 8 from Princeton's Fall 2012 midterm.