Heap Sort algorithm explained step-by-step: building a max-heap in O(n) time, extracting root elements, in-place heapify, complexity analysis, and JavaScript implementation.
Heap Sort is an in-place comparison-based sorting algorithm based on the Binary Heap data structure. A max-heap is a complete binary tree where the value of every parent node is greater than or equal to the values of its children, meaning the largest item always sits at the root (index 0).
The algorithm operates in two phases: first, it converts the unsorted array into a max-heap in O(n) linear time. Second, it repeatedly swaps the root maximum element with the last element of the heap and runs `heapify` on the reduced heap. Unlike Quick Sort, Heap Sort guarantees O(n log n) worst-case runtime and requires strictly O(1) auxiliary space.
Starting from the last non-leaf node (index Math.floor(n / 2) - 1) down to index 0, invoke heapify to establish the max-heap property throughout the entire array in O(n) time.
Swap the maximum element at the root (arr[0]) with the element at the boundary of the current unsorted heap (arr[i]).
Decrement the active heap size and call heapify on index 0 to sink the swapped element into its proper position, restoring the max-heap property.
Continue the extract-and-heapify process until the heap size reduces to 1. The array is now fully sorted in ascending order.
1function heapSort(arr) {2 const n = arr.length;3 4 // Step 1: Build max heap bottom-up in O(n) time5 for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {6 heapify(arr, n, i);7 }8 9 // Step 2: Extract elements one by one from heap10 for (let i = n - 1; i > 0; i--) {11 // Move current maximum (root) to the end12 [arr[0], arr[i]] = [arr[i], arr[0]];13 14 // Restore max heap on the reduced array15 heapify(arr, i, 0);16 }17 18 return arr;19}20 21function heapify(arr, n, i) {22 let largest = i;23 const left = 2 * i + 1;24 const right = 2 * i + 2;25 26 // Check if left child exists and is greater than largest27 if (left < n && arr[left] > arr[largest]) {28 largest = left;29 }30 31 // Check if right child exists and is greater than largest32 if (right < n && arr[right] > arr[largest]) {33 largest = right;34 }35 36 // If largest is not the current root node, swap and continue heapifying37 if (largest !== i) {38 [arr[i], arr[largest]] = [arr[largest], arr[i]];39 heapify(arr, n, largest);40 }41}42 43// Example usage:44// heapSort([4, 10, 3, 5, 1]); // [1, 3, 4, 5, 10]Play through every comparison, swap, and state change, adjust the speed, or enter custom inputs to test edge cases.
The full interactive roadmap is unlocking — these specialized modules land in upcoming releases.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
Explore the full catalog — 30+ algorithms with more unlocking every release.
Ready — press Play or use step controls to walk through the algorithm.
Comparisons
0
Swaps / Shifts
0
Passes / Depth
0
Elements
10
Test your understanding of Heap Sort complexity and step mechanics.
Q1.What is the time complexity to build a max-heap from an unsorted array of size n?
Q2.Is Heap Sort a stable sorting algorithm?
Apply Heap Sort to real coding interview questions.
Find the kth largest element in an unsorted array.