Max-heap property
Every node is greater than or equal to its children. Therefore the maximum of the entire heap is always at root A[0]. Siblings and separate subtrees do not need to be ordered relative to one another.
Sorting algorithms · 4/7
Builds a max heap and moves maxima to the end.
The root holds the maximum. Swap it with the active end, shrink the heap, and restore the heap property.
A binary heap is a complete binary tree satisfying a local ordering relation. “Complete” means that every level is full except possibly the last, which is filled from left to right. This shape guarantees Θ(log n) height.
Every node is greater than or equal to its children. Therefore the maximum of the entire heap is always at root A[0]. Siblings and separate subtrees do not need to be ordered relative to one another.
No pointers are needed: with zero-based indices, the children of A[i] are A[2i+1] and A[2i+2], while for i > 0 the parent of A[i] is A[⌊(i−1)/2⌋]. Indices beyond the current heap length are not part of the tree.
The array [9, 7, 6, 4, 3, 1, 5, 2] represents a max heap: A[0]=9 dominates 7 and 6; A[1]=7 dominates 4 and 3; the same property holds for every other parent.
The bar view shows swaps and the sorted region; the parallel representation links every array position to its corresponding max-heap node.
Use 2 to 16 values. This visualizer accepts values from 0 to 999.
heapSort(A) buildMaxHeap(A) for end ← length(A) − 1 downto 1 swap A[0], A[end] siftDown(A, 0, end)buildMaxHeap(A) for root ← ⌊length(A) / 2⌋ − 1 downto 0 siftDown(A, root, length(A))siftDown(A, root, end) while 2 · root + 1 < end left ← 2 · root + 1 right ← left + 1 largest ← root if A[left] > A[largest]: largest ← left if right < end and A[right] > A[largest]: largest ← right if largest = root: return swap A[root], A[largest] root ← largestThe highlighted line corresponds to the operation described by the visualizer. Index-management details are intentionally simplified.
The standard extraction phase performs n−1 heap repairs, Θ(n log n).
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.
Stability concerns the relative order of elements with equal keys.
This classification refers to the version shown on this page.
Useful for a Θ(n log n) guarantee with constant auxiliary space.
Bottom-up build-heap is Θ(n); n separate insertions would be Θ(n log n).