Loading algorithms…
Loading visualizer…
Sort in-place using a max-heap. Phase 1 builds the heap; phase 2 repeatedly extracts the max. Guaranteed O(n log n) with no extra memory.
The bar chart and the binary tree are the same data — the array is the heap's level-order storage.
Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure. It runs in two phases: first it builds a max-heap from the input array, then it repeatedly extracts the maximum and shrinks the heap. Invented by J. W. J. Williams in 1964, it's the canonical in-place O(n log n) sort.
0. We build it in O(n) by sifting down from the last internal node.arr[0] with the last element of the heap, then shrink the heap by one. The element we just moved to the end is now in its final sorted position.A binary heap can be stored in a plain array using level-order traversal:
No pointers, no auxiliary buffer — the heap lives entirely inside the input array. This is what makes heap sort truly in-place.
💡 Time complexity: Build heap is O(n) (not O(n log n) — most nodes sift down only a few levels). Each of the n − 1 extract operations does O(log n) work. Total: O(n log n). And unlike quicksort, it's guaranteed— no O(n²) worst case.
The tree view shows the implicit-binary-heap layout: index 0 is the root, indices 1 & 2 are the root's children, and so on.