44A. Heap Sort


Max-heaps

We have worked with min-heaps, which are sensible for priority queues. But now we need to use max-heaps, which have the same structural requirements as min-heaps, but have the following ordering requirement.

In a max-heap, the largest value is at the root.


Storing only the priority

To use heaps for priority queues, it is sensible to store an (item, priority) pair at each node. But for this page, let's get rid of the item entirely and only store the priority. The array is just an array of integers.

That requires some modifications to reheapUp and reheapDown, but those are easy to do. Let's call the new functions reheapUpMax and reheapDownMax.


Exercises

  1. Modify the definitions of reheapUp and reheapDown to get the definitions of reheapUpMax and reheapDownMax. Keep in mind that the array is now an array of integers, not an array of ItemAndPriority structures. Answer