Heap Sort Visualizer (Step-by-Step)
Watch heap sort build a max-heap and extract elements, with animated bars and every swap explained.
Start: build a max-heap.
Step-by-step working
- Build a max-heap bottom-up (sift-down from n/2 ā 1 to 0).
- Repeatedly swap the root with the last unsorted element and sift down.
- Press Play or Step to watch every swap.
Mathematical Formula
Overview & Explanation
Heap sort turns the array into a binary max-heap, then repeatedly moves the root (maximum) to the end.
How It Works
- Children of index i are 2i+1 and 2i+2.
- Sift down swaps a node with its larger child until the heap property holds.
- Each extraction shrinks the heap by one.
Practical Applications
- Learning priority queues.
- Interview preparation.
Example 1: 38, 27, 43, 3, 9, 82, 10
Seven elements.
Inputs
Array to sort (comma separated, max 40): 38, 27, 43, 3, 9, 82, 10
Step 1
Build a max-heap bottom-up (sift-down from n/2 ā 1 to 0).
Step 2
Repeatedly swap the root with the last unsorted element and sift down.
Step 3
Press Play or Step to watch every swap.
Sorted
Result
Example 2: already sorted 1..6
Heap sort still does O(n log n) work.
Inputs
Array to sort (comma separated, max 40): 1, 2, 3, 4, 5, 6
Step 1
Build a max-heap bottom-up (sift-down from n/2 ā 1 to 0).
Step 2
Repeatedly swap the root with the last unsorted element and sift down.
Step 3
Press Play or Step to watch every swap.
Sorted
Result
How it's calculated & sources
Build heap: for i = ān/2āā1 ⦠0, siftDown(i). Then for end = nā1 ⦠1: swap(0, end), siftDown(0, end). Time O(n log n), space O(1).
Frequently Asked Questions
Is heap sort stable?
Why O(n) heap construction?
Heap sort vs quick sort?
Related Calculators
Quick Sort Visualizer & Partition Calculator
Step-by-step Quick Sort partition visualizer demonstrating pivot selection, left/right pointers, and recursion tree.
Merge Sort Visualizer
Interactive divide and conquer Merge Sort step generator showing array splitting and merging.
Shell Sort Visualizer (Step-by-Step)
Animate Shell sort with shrinking gaps and see each gapped insertion move.
Bubble Sort Visualizer & Step Calculator
Interactive step-by-step Bubble Sort execution visualizer with comparisons, swaps, and time complexity breakdown.