Heap Sort Algorithm in DSA C - Time & Space Complexity
We want to understand how the time taken by Heap Sort changes as the input size grows.
Specifically, how many steps does Heap Sort need to sort a list of numbers?
Analyze the time complexity of the following code snippet.
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int 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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
This code builds a max heap from the array, then repeatedly extracts the largest element to sort the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
heapifyfunction called multiple times to maintain the heap property. - How many times: First,
heapifyis called for each non-leaf node once (about n/2 times). Then, it is called once per element during sorting (n times), each call possibly recursing down the tree.
As the input size n grows, the number of operations grows roughly proportional to n times the height of the heap, which is about log n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 x 3 = 30 |
| 100 | About 100 x 7 = 700 |
| 1000 | About 1000 x 10 = 10,000 |
Pattern observation: When input size increases 10 times, operations increase about 10 x log(10) times, which is more than 10 but much less than 100.
Time Complexity: O(n log n)
This means the time to sort grows a bit faster than the input size, but not as fast as squaring it.
[X] Wrong: "Heap Sort is as slow as bubble sort because it uses loops inside loops."
[OK] Correct: Heap Sort uses a smart tree structure that limits how far elements move, so it does fewer total steps than simple nested loops.
Understanding Heap Sort's time complexity shows you can analyze efficient sorting methods, a key skill for many coding challenges and real-world tasks.
"What if we changed the heap from a max heap to a min heap? How would the time complexity change?"
Practice
Heap Sort Algorithm?Solution
Step 1: Understand heapify's role
Heapify rearranges elements to satisfy heap property (max or min heap).Step 2: Connect heapify to heap sort
Heap sort uses heapify to build the heap before sorting.Final Answer:
To build a max heap or min heap from an unsorted array -> Option AQuick Check:
Heapify builds heap = To build a max heap or min heap from an unsorted array [OK]
- Confusing heapify with swapping elements
- Thinking heapify reverses the array
- Assuming heapify counts elements
arr[i] and arr[j] in C during heap sort?Solution
Step 1: Recall C swap syntax
In C, swapping requires a temporary variable to hold one value.Step 2: Check each option
temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; uses temp variable correctly; others are invalid in C.Final Answer:
temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option BQuick Check:
Use temp variable to swap in C = temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; [OK]
- Trying Python-style swap syntax in C
- Assigning without temp causing overwrite
- Using undefined swap function
arr = [4, 10, 3, 5, 1]?Solution
Step 1: Build max heap from array
Max heap of [4,10,3,5,1] rearranges to [10,5,3,4,1].Step 2: Extract max and sort
Repeatedly swap max with last element and heapify to get sorted array [1,3,4,5,10].Final Answer:
[1, 3, 4, 5, 10] -> Option CQuick Check:
Heap sort output = sorted array ascending = [1, 3, 4, 5, 10] [OK]
- Confusing max heap with sorted array
- Forgetting to heapify after swaps
- Assuming heap sort outputs descending order
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2*i;
int right = 2*i + 1;
if (left < n && arr[left] > arr[largest]) largest = left;
if (right < n && arr[right] > arr[largest]) largest = right;
if (largest != i) {
int temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp;
heapify(arr, n, largest);
}
}Solution
Step 1: Check child index calculation
In 0-based arrays, left child = 2*i + 1, right child = 2*i + 2.Step 2: Verify other parts
Swap and recursion are correct; base case is implicit by condition checks.Final Answer:
Left and right child indices should be 2*i + 1 and 2*i + 2 -> Option DQuick Check:
Child indices in 0-based heap = 2*i+1, 2*i+2 [OK]
- Using 1-based indexing formulas in 0-based arrays
- Ignoring recursive heapify call
- Misplacing swap logic
Solution
Step 1: Understand stability of heap sort
Heap sort is not stable because it swaps elements without preserving order of equal keys.Step 2: Compare performance with quicksort
Heap sort guarantees O(n log n) time always; quicksort is faster on average but can degrade to O(n²).Final Answer:
Heap sort is not stable but has consistent O(n log n) time; quicksort is unstable and may degrade to O(n²) -> Option AQuick Check:
Heap sort stable? No; Time? O(n log n) always [OK]
- Assuming heap sort is stable
- Thinking quicksort worst-case is always O(n log n)
- Confusing stability with speed
Ready to prove it?
Pass the test and this pattern moves unknown
