Heap sort uses a binary heap stored inside the array itself. First it rearranges the array into a max-heap, where every parent is at least as large as its children, so the largest value sits at index 0. Then it swaps that value to the end of the array, shrinks the heap by one and sifts the new top down to repair the heap, repeating until the heap is empty.
This visualizer shows each parent (purple) being compared with its children (orange) and the swaps in red, while the sorted tail grows in green from the right. To see the same heap drawn as a tree, open the binary heap visualizer.
How to use the Heap Sort Visualizer tool
- Pick an array size and an input: random, nearly sorted, reversed or few unique values. You can also type your own numbers.
- Press Play to watch the sort, or Step to move one comparison or swap at a time. Back goes one step back.
- Follow the colours: orange bars are being compared, red bars are being swapped or written, green bars are in their final place.
- Read the message under the controls and the highlighted pseudocode line to see exactly what the algorithm is doing.
- Change the speed, switch the code tab to C++, Java, Python or JavaScript, and compare the counters with other inputs.
Why use ToolOrca Heap Sort Visualizer
- Animated bars with play, pause, step and step back
- Speed slider and a position slider to jump to any step
- Comparison and swap counters
- Random, nearly sorted, reversed and few-unique inputs, or your own numbers
- Highlighted pseudocode line for every step
- Code in C++, Java, Python and JavaScript
- Works on phones and tablets, keyboard accessible
How heap sort works
Phase 1, build the heap: for i from n/2 − 1 down to 0, sift a[i] down: compare it with its children at 2i + 1 and 2i + 2 and swap it with the larger child while that child is larger. Leaves need no work, so the loop starts at the last parent. Phase 2, sort: for end from n − 1 down to 1, swap a[0] (the maximum) with a[end], then sift a[0] down within the first end elements.
Time and space complexity of heap sort
Building the heap takes O(n). Each of the n − 1 extractions sifts down at most log₂ n levels, so the total is O(n log n) in the best, average and worst case. Heap sort sorts in place with O(1) extra memory, which neither merge sort nor quick sort can fully claim. It is not stable, and it jumps around the array, so it uses the CPU cache less well than quick sort.
When to use heap sort
Heap sort is the safe choice when you need guaranteed O(n log n) time with constant extra memory, for example in embedded systems. It is also the fallback in introsort (C++ std::sort) when quick sort’s recursion gets too deep. If you only need the k largest values, stop after k extractions.
Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.
Frequently asked questions
What is the time complexity of heap sort?
O(n log n) in every case: O(n) to build the heap plus n extractions of O(log n) each.
Is heap sort stable?
No. Swapping the root with the last element and sifting down can reorder equal values.
Why does heap sort use a max-heap to sort in ascending order?
The maximum is removed first and placed at the end of the array, so the sorted part grows from right to left in ascending order.
Heap sort vs quick sort: which is better?
Quick sort is usually faster in practice, but heap sort guarantees O(n log n) and uses O(1) extra memory. Introsort combines them.