The standard extraction phase performs n−1 heap repairs, Θ(n log n).
Sorting algorithms · 4/7
Heapsort
Builds a max heap and moves maxima to the end.
How it works
The root holds the maximum. Swap it with the active end, shrink the heap, and restore the heap property.
Step-by-step visualization
Each bar represents one element. Colors identify compared elements, the pivot or key, the final region and the active range.
Use 2 to 16 values. This visualizer accepts values from 0 to 999.
- Comparisons
- 0
- Writes
- 0
- Swaps
- 0
- Pass / level
- 0
Pseudocode linked to the visualization
buildMaxHeap(A)for end ← n − 1 downto 1swap A[0], A[end]siftDown(A, 0, end)siftDown(A, root, end)child ← 2 · root + 1choose the larger childif child > root: swap and continue
The highlighted line corresponds to the operation described by the visualizer. Index-management details are intentionally simplified.
Complexity analysis
Build-heap is Θ(n); Θ(n log n) extraction dominates.
Heap height is always Θ(log n), so worst time stays Θ(n log n).
The iterative form uses Θ(1) auxiliary space. Long-distance swaps destroy stability.
Properties at a glance
Stability concerns the relative order of elements with equal keys.
This classification refers to the version shown on this page.
When to use it and what to avoid
A good choice when…
Useful for a Θ(n log n) guarantee with constant auxiliary space.
Common mistake
Bottom-up build-heap is Θ(n); n separate insertions would be Θ(n log n).