Heap Sort Algorithm: Complete Implementation Guide with Python & JavaScript [2024] | Day #16
Heap Sort algorithm with step-by-step Python and JavaScript implementations
![Heap Sort Algorithm: Complete Implementation Guide with Python & JavaScript [2024] | Day #16](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1728051161984%2F4c7db2c9-6fcf-4022-8c9a-69f3076b723b.png&w=3840&q=75)
Key Takeaways
Discuss Heap Sort implementation with O(n log n) time complexity
Understand heap data structures and the heapify process
Learn when to choose Heap Sort over other sorting algorithms
Get practical code examples in Python and JavaScript
Explore real-world applications and optimization techniques
%[https://youtu.be/0-0tIyXncjc?feature=shared]
Introduction to Heap Sort
Heap Sort is an efficient, comparison-based sorting algorithm that uses a binary heap data structure to sort elements. It combines the speed of Quick Sort with the consistent performance of Merge Sort, making it an excellent choice for systems requiring guaranteed O(n log n) time complexity.
Why Learn Heap Sort?
Guaranteed O(n log n) time complexity
In-place sorting algorithm
No worst-case scenarios like Quick Sort
Essential for understanding priority queues
Common in technical interviews
Understanding Heaps
Before diving into Heap Sort, let's understand the heap data structure:
90 Max Heap Example
/ \
80 70
/ \ /
50 60 65
Heap Properties
Complete binary tree
Parent nodes are greater (max-heap) or smaller (min-heap) than children
Can be efficiently represented in an array
How Heap Sort Works
Heap Sort operates in two main phases:
Build Max Heap: Transform input array into max heap
Extract Elements: Repeatedly remove the maximum element
Visual Example of Heap Sort Process:
Initial Array: [4, 10, 3, 5, 1]
Step 1 - Build Max Heap:
10
/ \
5 3
/ \
4 1
Step 2 - Extract Max:
1. [10, 5, 3, 4, 1] → [1, 5, 3, 4, |10]
2. [5, 4, 3, 1, |10] → [1, 4, 3, |5, 10]
3. [4, 1, 3, |5, 10] → [1, 3, |4, 5, 10]
4. [3, 1, |4, 5, 10] → [1, |3, 4, 5, 10]
Final Sorted Array: [1, 3, 4, 5, 10]
Implementation Guide
Python Implementation
class HeapSort:
def __init__(self):
self.heap_size = 0
def heapify(self, arr, n, i):
"""
Maintain heap property for subtree rooted at index i.
Args:
arr: Array to heapify
n: Size of heap
i: Root index of subtree
"""
largest = i
left = 2 * i + 1
right = 2 * i + 2
# Compare with left child
if left < n and arr[left] > arr[largest]:
largest = left
# Compare with right child
if right < n and arr[right] > arr[largest]:
largest = right
# Swap if needed and recursively heapify
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
self.heapify(arr, n, largest)
def sort(self, arr):
"""
Sort array using Heap Sort algorithm.
Args:
arr: Array to sort
Returns:
Sorted array
"""
n = len(arr)
# Build max heap
for i in range(n // 2 - 1, -1, -1):
self.heapify(arr, n, i)
# Extract elements from heap
for i in range(n - 1, 0, -1):
arr[0], arr[i] = arr[i], arr[0]
self.heapify(arr, i, 0)
return arr
JavaScript Implementation
class HeapSort {
constructor() {
this.heapSize = 0;
}
heapify(arr, n, i) {
let largest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
this.heapify(arr, n, largest);
}
}
sort(arr) {
const n = arr.length;
// Build max heap
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
this.heapify(arr, n, i);
}
// Extract elements from heap
for (let i = n - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]];
this.heapify(arr, i, 0);
}
return arr;
}
}
Performance Analysis
Time Complexity
| Operation | Time Complexity |
| Build Heap | O(n) |
| Heapify | O(log n) |
| Overall | O(n log n) |
Space Complexity
In-place sorting: O(1) auxiliary space
Recursive call stack: O(log n)
Comparison with Other Sorting Algorithms
| Algorithm | Time (Avg) | Time (Worst) | Space | Stable |
| Heap Sort | O(n log n) | O(n log n) | O(1) | No |
| Quick Sort | O(n log n) | O(n²) | O(log n) | No |
| Merge Sort | O(n log n) | O(n log n) | O(n) | Yes |
| Bubble Sort | O(n²) | O(n²) | O(1) | Yes |
Practical Applications
Priority Queues
class PriorityQueue(HeapSort): def insert(self, item): # Implementation detailsOperating System Task Scheduling
Process scheduling
Memory management
I/O request handling
External Sorting
Sorting large files
Database operations
Optimization Strategies
- Iterative Heapify
def iterative_heapify(arr, n, i):
while True:
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[left] > arr[largest]:
largest = left
if right < n and arr[right] > arr[largest]:
largest = right
if largest == i:
break
arr[i], arr[largest] = arr[largest], arr[i]
i = largest
Bottom-up Heap Construction
Cache-Friendly Implementations
Common Pitfalls & Solutions
Array Index Calculation
# Correct way left = 2 * i + 1 right = 2 * i + 2 parent = (i - 1) // 2Heap Size Management
Track heap size separately
Update after each extraction
Edge Cases
def handle_edge_cases(arr): if not arr or len(arr) <= 1: return arr
FAQs
Q1: Why isn't Heap Sort stable?
A: Heap Sort may change the relative order of equal elements due to the heap structure and extraction process.
Q2: When should I use Heap Sort over Quick Sort?
A: Use Heap Sort when you need guaranteed O(n log n) performance and memory usage is a concern.
Q3: Can Heap Sort be parallelized?
A: The heapify process can be partially parallelized, but the sequential nature of extraction limits full parallelization.
Summary & Next Steps
Key Points
Heap Sort provides guaranteed O(n log n) performance
In-place sorting with O(1) extra space
Not stable but efficient for large datasets
Practice Exercises
Implement a priority queue using heaps
Sort large files using external sorting
Optimize heap operations for specific use cases
Further Learning
Advanced heap variants (Fibonacci heaps)
Priority queue applications
Parallel sorting algorithms
Resources & References
Introduction to Algorithms by Cormen et al.
Advanced Data Structures by Peter Brass
Algorithm Design Manual by Steven Skiena




