Skip to content

Building a heap and heapsort

Build a heap from any array in O(n) by sifting down from the last parent to the root, and sort an array in place with heapsort in O(n log n), tracing both.

It opens on

This tree of nine numbers is not yet a heap with the largest key on top. Should we repair it starting at the root, 3, or at the parents near the bottom?

Why it matters

Pushing n keys one at a time costs about n log n, and heapify does the same job in n steps, which is why every library builds its heaps this way, and heapsort turns that heap into a sort that needs no extra array and has no bad input.

Good to know first

  • Removing the minimum: sift down swaps a key with its smaller child until it is at most its children, in O(log n)
  • Binary heaps and insertion: a heap is a complete tree in an array, with the parent of i at (i − 1)//2
  • Merge sort: O(n log n) sorting, with Θ(n) extra space
  • Selection and insertion sort: a sort is stable if equal keys keep their order

Step by step

  1. Heapify from the bottom

    Turn an arbitrary array into a max-heap by calling sift down at each parent from the last one back to index 0, and trace it on nine keys.

    Index 3 holds 7, and its children at 7 and 8 hold 6 and 4, so the larger child is 6, and 7 is at least 6, so 7 stays where it is.

    A common mix-up

    Heapify should start at the root, since the root is the one that is wrong.

    In fact: The repair at the root assumes both subtrees are heaps, so it must come last, after all the parents below it.

  2. Why it takes O(n)

    Show that heapify makes at most n swaps and about 2n comparisons by counting the work by the height of each node, and compare it with building by pushes.

    A common mix-up

    Heapify is O(n log n) because it makes n/2 sift downs of O(log n) each.

    In fact: That bound is true but not tight; a sift down from height h costs at most h, and the heights sum to under n.

  3. Heapsort

    Sort an array in place by heapifying it and then swapping the root with the last key of the heap n − 1 times, trace it, and state its cost, extra space and stability.

    The heap is 9, 7, 8, 6, 5, 2, 1, 3, 4. Swap a[0] with a[8], and the array is 4, 7, 8, 6, 5, 2, 1, 3, 9, with 9 final. Sift down the 4 within the first 8 keys. It swaps with the larger child 8 and stops above 2 and 1, giving 8, 7, 4, 6, 5, 2, 1, 3, 9.

    the first two rows of the table

    A common mix-up

    Heapsort is a stable sort, since it uses a heap like a queue.

    In fact: The swap of the root with the last key moves equal keys past each other, and the run reversed the order of b and a.

Try it

Heapify the array 1, 5, 3 as a max-heap. What is the array afterwards?Answer

5, 1, 3

Hint: There is only one parent, at index 0, and its larger child is 5

A perfect heap has 7 keys, 4 leaves, 2 nodes of height 1 and 1 of height 2. What is the most swaps heapify can make?Answer

4

Hint: 2 × 1 plus 1 × 2

Heapsort the array 2, 5, 1. What is the array after heapify, after the first swap and sift down, and at the end?Answer

5, 2, 1; then 2, 1, 5; then 1, 2, 5

Hint: After heapify the root 5 is swapped with the last key 1

Index 1 holds 9, and its children hold 7 and 5. What happens at index 1?Answer

Nothing changes, since 9 is at least 7 and 5.

A perfect heap has 31 keys, with 16 leaves, 8 nodes of height 1, 4 of height 2, 2 of height 3 and 1 of height 4. What is the most swaps heapify can make?Answer

26, since 8 × 1 plus 4 × 2 plus 2 × 3 plus 1 × 4 is 8 plus 8 plus 6 plus 4.

Building a heap and heapsort

30 minutes every month. No card.