Overview
Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure. It divides its input into a sorted and an unsorted region, and iteratively shrinks the unsorted region by extracting the largest element and inserting it into the sorted region. Heap Sort combines the better attributes of merge sort (O(n log n) worst case) and insertion sort (in-place).Implementation
How It Works
- Build Max Heap: Transform the array into a max heap structure where parent nodes are greater than their children
- Extract Maximum: Swap the root (maximum element) with the last element in the heap
- Reduce Heap Size: Decrease the heap size by one
- Heapify: Restore the heap property for the reduced heap
- Repeat: Continue steps 2-4 until the heap is empty
A max heap is a complete binary tree where each node is greater than or equal to its children. In array representation, for element at index i:
- Left child is at index 2i + 1
- Right child is at index 2i + 2
- Parent is at index ⌊(i-1)/2⌋
Complexity Analysis
Time Complexity
- Best Case: O(n log n)
- Average Case: O(n log n)
- Worst Case: O(n log n)
- Guaranteed performance in all cases
Space Complexity
- Space: O(1) - sorts in place
- Auxiliary: O(1) - only uses a few variables
- Call Stack: O(log n) - for heapify recursion
Usage Example
Visual Example
Let’s trace through sorting[4, 10, 3, 5, 1]:
Characteristics
Stability
Stability
Heap Sort is not stable. Equal elements may not maintain their relative order due to the heap operations.
In-Place
In-Place
Yes, Heap Sort is in-place. It only requires O(1) additional space (excluding the recursion stack).
Adaptive
Adaptive
No, Heap Sort is not adaptive. It always performs O(n log n) operations regardless of the input order.
Online
Online
No, Heap Sort is not online. It needs access to all elements to build the initial heap.
When to Use
Heap Sort is excellent when you need guaranteed O(n log n) performance with O(1) space.
- Situations requiring guaranteed O(n log n) worst-case performance
- Memory-constrained environments (in-place sorting)
- Systems where predictable performance is critical
- Real-time systems with strict time bounds
- When you can’t afford the O(n²) worst case of Quick Sort
- Cache performance is critical (poor cache locality)
- Stable sorting is required
- Average case performance matters more than worst case (Quick Sort is typically faster)
- You’re sorting small arrays (simpler algorithms may be faster)
Performance Insights
Cache Performance
Code Walkthrough
Heapify Function
Main Function
Heap Operations Time Complexity
| Operation | Time Complexity | Description |
|---|---|---|
| Build Heap | O(n) | Convert array to heap (not O(n log n)!) |
| Heapify | O(log n) | Restore heap property |
| Extract Max | O(log n) | Remove root and heapify |
| Insert | O(log n) | Add element and heapify up |
Why is building a heap O(n)?While heapify is O(log n), building a heap from scratch is O(n), not O(n log n). This is because:
- Half the nodes are leaves (no heapify needed)
- Quarter of nodes have height 1
- Most nodes are near the bottom
- Sum of heights: n/2 × 0 + n/4 × 1 + n/8 × 2 + … = O(n)
Comparison with Other Sorts
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable | Cache-Friendly |
|---|---|---|---|---|---|---|
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | No |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | Yes |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | Yes |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
Use in Priority Queues
The heap data structure used in Heap Sort is the foundation for priority queues, which are used in:
- Dijkstra’s shortest path algorithm
- Prim’s minimum spanning tree
- Event scheduling in simulations
- Huffman coding
- Memory management