Heap sort (Williams)

Published

2023-08-05

Remember binary heaps? Remember using binary heaps as a priority queue? Heap sort algorithm uses a binary heap to store unsorted values. Where’s the next element in my sorted list? Right at the root of the binary heap! Heap sort takes an unsorted input vector, transforms it into a binary heap, and then, while elements remain in the heap, it extracts them one at a time adding to the sorted portion of the vector and then it uses the percolation method to restore the heap order property. Heap sort has average case and worst case complexity of \mathcal{O}(n \log n).

Here we implement heap sort in C++.

Readings and supplemental materials:

Comprehension check:

  1. The height of a binary tree with n elements is ____________.
  2. A binary heap is a binary tree with the ________________ property and the _________________ property.
  3. True or false? A binary heap is easily represented as a vector of elements.
  4. If we have a max heap, the maximum value in the heap appears at the _____________.
  5. After removing the root from a heap we must restore the ________________ property.
  6. The worst case time complexity for heap sort is __________________.

Answers: (u ƃol u)O / ɹǝpɹo dɐǝɥ / ʇooɹ / ǝnɹʇ / ɹǝpɹo dɐǝɥ / ǝɹnʇɔnɹʇs / u ƃol

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.