Skip to content
StackDecode
SortingAnimated

Heap Sort

Build a max-heap, then repeatedly extract the maximum.

Time

O(n log n)

Extra space

O(1)

Use it for

Guaranteed O(n log n) with no extra memory.

How it works

  1. Rearrange the list into a max-heap (every parent ≥ its children).
  2. The largest value is at the root: swap it with the last item and shrink the heap.
  3. Sift the new root down to restore the heap.
  4. Repeat. O(n log n), in place, not stable.

Watch it run

Loading animation…