Heapsort
Heapsort uses a binary heap to sort an array in time. It can sort the array in place, with auxiliary space.
After reading this chapter, you should be able to:
- Sort an array through a priority queue in , as a faster version of selection sort
- Rearrange an array into a heap in place by sinking each internal node from the bottom up
- Show that the bottom-up build is by bounding each node’s sink by its height rather than by the height of the tree
- Implement in-place heapsort as a max-heap build followed by a sortdown
- Show that heapsort runs in time with auxiliary space
- Compare heapsort with the other sorts by time, space, stability, and cache locality
You can download the accompanying code for this chapter: starter and solution.
Sections
- Sorting with a Priority Queue
- Tracing the Priority Queue Sort
- Building a Heap from an Array
- Using sink to Build a Heap
- Tracing the Build
- Building the Heap in Code
- Emptying the Heap
- Two Changes to sink
- The Complete Sort
- Tracing the Complete Sort
- Why the Build Is Linear
- In-Place and Stable?
- Heapsort and Cache Locality