1. Home
  2. Design & Analysis of Algorithms
  3. Heap Sort

Heap Sort

Build a max-heap, then repeatedly move the biggest element to the end. Guaranteed O(n log n) with no extra memory — see the tree and the array change together.

Interactive 3DIntermediate11 min readDAAUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Heap sort uses a max-heap: a complete tree (top) where every parent ≥ its children, stored in the array (bars). Step 1: turn the array into a max-heap.

Pseudocode

  1. procedure heapSort(a):
  2. for i ← n/2 − 1 down to 0: siftDown(a, i, n) // build max-heap
  3. for end ← n − 1 down to 1:
  4. swap(a[0], a[end]) // biggest goes to the end
  5. siftDown(a, 0, end) // repair the smaller heap
  6. procedure siftDown(a, i, size):
  7. while i has a child inside size:
  8. c ← the larger child
  9. if a[i] ≥ a[c]: stop
  10. swap(a[i], a[c]); i ← c
Read this model as text · 43 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Heap sort uses a max-heap: a complete tree (top) where every parent ≥ its children, stored in the array (bars). Step 1: turn the array into a max-heap.
  2. Build heap (i = 3): larger child of 8 is 15, and 8 < 15, so swap them.
  3. 15 moves up, 8 sinks down.
  4. Build heap (i = 2): 12 ≥ its larger child 8 — the heap rule holds, stop.
  5. Build heap (i = 1): larger child of 5 is 15, and 5 < 15, so swap them.
  6. 15 moves up, 5 sinks down.
  7. Build heap (i = 1): larger child of 5 is 8, and 5 < 8, so swap them.
  8. 8 moves up, 5 sinks down.
  9. Build heap (i = 0): larger child of 12 is 15, and 12 < 15, so swap them.
  10. 15 moves up, 12 sinks down.
  11. Build heap (i = 0): 12 ≥ its larger child 8 — the heap rule holds, stop.
  12. Max-heap built: the biggest value 15 is at the root (a[0]).
  13. The root 15 is the largest remaining value. Swap it with the last heap element 5 (a[7]).
  14. 15 is now in its final place (green) and leaves the heap. The heap shrinks to 7.
  15. Sift down: larger child of 5 is 12, and 5 < 12, so swap them.
  16. 12 moves up, 5 sinks down.
  17. Sift down: larger child of 5 is 8, and 5 < 8, so swap them.
  18. 8 moves up, 5 sinks down.
  19. The root 12 is the largest remaining value. Swap it with the last heap element 5 (a[6]).
  20. 12 is now in its final place (green) and leaves the heap. The heap shrinks to 6.
  21. Sift down: larger child of 5 is 12, and 5 < 12, so swap them.
  22. 12 moves up, 5 sinks down.
  23. Sift down: larger child of 5 is 8, and 5 < 8, so swap them.
  24. 8 moves up, 5 sinks down.
  25. The root 12 is the largest remaining value. Swap it with the last heap element 5 (a[5]).
  26. 12 is now in its final place (green) and leaves the heap. The heap shrinks to 5.
  27. Sift down: larger child of 5 is 8, and 5 < 8, so swap them.
  28. 8 moves up, 5 sinks down.
  29. Sift down: 5 ≥ its larger child 5 — the heap rule holds, stop.
  30. The root 8 is the largest remaining value. Swap it with the last heap element 3 (a[4]).
  31. 8 is now in its final place (green) and leaves the heap. The heap shrinks to 4.
  32. Sift down: larger child of 3 is 8, and 3 < 8, so swap them.
  33. 8 moves up, 3 sinks down.
  34. The root 8 is the largest remaining value. Swap it with the last heap element 5 (a[3]).
  35. 8 is now in its final place (green) and leaves the heap. The heap shrinks to 3.
  36. Sift down: 5 ≥ its larger child 5 — the heap rule holds, stop.
  37. The root 5 is the largest remaining value. Swap it with the last heap element 3 (a[2]).
  38. 5 is now in its final place (green) and leaves the heap. The heap shrinks to 2.
  39. Sift down: larger child of 3 is 5, and 3 < 5, so swap them.
  40. 5 moves up, 3 sinks down.
  41. The root 5 is the largest remaining value. Swap it with the last heap element 3 (a[1]).
  42. 5 is now in its final place (green) and leaves the heap. The heap shrinks to 1.
  43. Sorted! Heap sort is O(n log n) in every case and needs no extra array (in-place) — but it is not stable.

Try this in the 3D model

  • Watch the build-heap phase. After it, which value is at the root?
  • Follow one value as it is swapped to the end — it turns green and leaves the tree.
  • Load Already sorted. Does heap sort get any faster? (Compare with insertion sort.)

The idea

Heap sort uses a binary heap — specifically a max-heap, where every parent is ≥ its children, so the biggest value is always at the root.

  1. Build a max-heap out of the array.
  2. The largest value is now at a[0]. Swap it with the last element of the heap — it’s now in its final position.
  3. Shrink the heap by one and sift the new root down to restore the heap rule.
  4. Repeat until the heap is empty. The array is sorted, smallest to largest.

The 3D model shows the heap as a tree (top) and the same data as bars (front). Green bars have reached their final place.

Sift down

To repair the heap at index i: compare a[i] with its children (2i + 1 and 2i + 2). If the larger child is bigger than a[i], swap them and continue from the child’s position. Each swap moves down one level, so sift-down is O(log n).

Building the heap in O(n)

Leaves are already tiny heaps, so start at the last parent (n/2 − 1) and sift down every node back to the root. Surprisingly, this costs only O(n) in total, because most nodes are near the bottom and sift down only a little.

Code

def sift_down(a, i, size):
    while True:
        l, r = 2 * i + 1, 2 * i + 2
        largest = i
        if l < size and a[l] > a[largest]: largest = l
        if r < size and a[r] > a[largest]: largest = r
        if largest == i:
            return
        a[i], a[largest] = a[largest], a[i]
        i = largest

def heap_sort(a):
    n = len(a)
    for i in range(n // 2 - 1, -1, -1):     # build max-heap
        sift_down(a, i, n)
    for end in range(n - 1, 0, -1):
        a[0], a[end] = a[end], a[0]          # largest to the end
        sift_down(a, 0, end)                 # repair the smaller heap
    return a

print(heap_sort([12, 3, 17, 8, 34, 25, 1]))  # [1, 3, 8, 12, 17, 25, 34]
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

void siftDown(vector<int>& a, int i, int size) {
    while (true) {
        int l = 2 * i + 1, r = l + 1, largest = i;
        if (l < size && a[l] > a[largest]) largest = l;
        if (r < size && a[r] > a[largest]) largest = r;
        if (largest == i) return;
        swap(a[i], a[largest]);
        i = largest;
    }
}

void heapSort(vector<int>& a) {
    int n = a.size();
    for (int i = n / 2 - 1; i >= 0; i--) siftDown(a, i, n);
    for (int end = n - 1; end > 0; end--) {
        swap(a[0], a[end]);
        siftDown(a, 0, end);
    }
}

int main() {
    vector<int> a = {12, 3, 17, 8, 34, 25, 1};
    heapSort(a);
    for (int x : a) cout << x << " ";
}

Heap sort vs merge sort vs quick sort

Heap sort Merge sort Quick sort
Worst case O(n log n) O(n log n) O(n²)
Extra space O(1) O(n) O(log n)
Stable ❌ ✅ ❌
Speed in practice Slower (jumps around in memory) Good Usually fastest

Heap sort’s big strength is the guarantee: O(n log n) time and O(1) space, always. That’s why C++’s std::sort (introsort) falls back to heap sort if quick sort starts going badly.

Common mistakes

  • Using a min-heap and expecting ascending order in place (you’d get descending).
  • Sifting down with the full size n after shrinking — use the current heap size end.
  • Starting build-heap at index n − 1 instead of n/2 − 1 (works, but wastes time on leaves).

Complexity at a glance

Case / operationTimeWhy
Best caseO(n log n)Every extraction still sifts down.
Average caseO(n log n)
Worst caseO(n log n)Guaranteed — unlike quick sort.
Build heapO(n)Bottom-up heapify.
Extra spaceO(1)

Quick check

Test yourself — pick an answer to see if you got it.

1. In a max-heap, where is the largest element?

2. After swapping the root with the last heap element, what must happen?

3. Is heap sort stable?

4. What is the extra memory needed by heap sort?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Heap Sort. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.