Heap Sort
Heap Sort leverages a heap: build a max-heap (or min-heap), repeatedly take the top element to the end, adjust the remaining heap, and continue until sorted.
Click "Step forward" to watch the algorithm state change.
- Build a max-heap from the array.
- Swap heap top (max) with the last element; heap size minus 1.
- Heapify the remaining elements.
- Repeat steps 2–3 until one element remains.
Pseudo-code:
function heapSort(A, n):
buildMaxHeap(A, n)
for i = n-1 downto 1:
swap(A[0], A[i])
heapify(A, 0, i)
function buildMaxHeap(A, n):
for i = n/2-1 downto 0:
heapify(A, i, n)
function heapify(A, i, n):
largest = i
l = 2*i+1
r = 2*i+2
if l < n and A[l] > A[largest]:
largest = l
if r < n and A[r] > A[largest]:
largest = r
if largest != i:
swap(A[i], A[largest])
heapify(A, largest, n)
- A heap is a complete binary tree, stored in an array
- Max-heap: the value of every node is greater than or equal to its children, so the top holds the maximum
- Min-heap: the value of every node is less than or equal to its children, so the top holds the minimum
- The left child of node is , the right child is , and the parent is
// Adjust the subtree rooted at i so that it becomes a max-heap
void Heapify(int A[], int i, int n) {
int largest = i;
int l = 2 * i + 1, r = 2 * i + 2;
if (l < n && A[l] > A[largest]) largest = l;
if (r < n && A[r] > A[largest]) largest = r;
if (largest != i) {
int tmp = A[i]; A[i] = A[largest]; A[largest] = tmp;
Heapify(A, largest, n); // adjust the subtree recursively
}
}
// Build the heap: start from the last non-leaf node and sift down
void BuildMaxHeap(int A[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
Heapify(A, i, n);
}
// Heap sort
void HeapSort(int A[], int n) {
BuildMaxHeap(A, n);
for (int i = n - 1; i > 0; i--) {
int tmp = A[0]; A[0] = A[i]; A[i] = tmp; // move the top (max) to the end
Heapify(A, 0, i); // adjust the remaining part
}
}
Heap Sort and Priority Queues
The heap is the most common implementation of a priority queue: enqueueing (insertion) costs and dequeueing (taking out the maximum or the minimum) costs . Dijkstra’s algorithm, task scheduling, and Top K problems all make use of the heap.
- Best/worst/avg:
- Space:
- Stability: unstable
Exercise 1
For , show the initial heap and the state after the first swap.
Approach: Build a max heap first, then swap.
Steps:
- Initial heap: [10, 5, 3, 4, 1]
- First swap: [1, 5, 3, 4, 10], adjusted into a new heap: [5, 4, 3, 1, 10]
Answer: As shown above; the maximum element is moved to its final position.
Exercise 2
What are heap sort’s time and space complexities?
Approach: Consider the time complexity and the extra space.
Steps:
- Time complexity
- Space complexity
Answer: Time , space .
