Heap Sort Algorithm in DSA C# - Time & Space Complexity
We want to understand how the time taken by Heap Sort changes as the list size grows.
Specifically, how the sorting steps increase when we have more numbers to sort.
Analyze the time complexity of the following Heap Sort code.
void HeapSort(int[] arr) {
int n = arr.Length;
for (int i = n / 2 - 1; i >= 0; i--) {
Heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
Swap(arr, 0, i);
Heapify(arr, i, 0);
}
}
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) {
Swap(arr, i, largest);
Heapify(arr, n, largest);
}
}
This code builds a max heap from the array, then repeatedly swaps the largest element to the end and rebuilds the heap.
Look for loops and recursive calls that repeat work.
- Primary operation: The Heapify function, which rearranges elements to maintain the heap.
- How many times: Heapify is called once for each non-leaf node during heap building, and then once per element during sorting, each possibly recursing down the tree.
As the number of elements grows, the number of Heapify calls and their depth grow too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 40 Heapify steps |
| 100 | About 700 to 800 Heapify steps |
| 1000 | About 10,000 Heapify steps |
Pattern observation: The operations grow a bit faster than just the number of elements, but not as fast as their square.
Time Complexity: O(n log n)
This means the time to sort grows a little more than linearly, roughly proportional to the number of elements times the height of the heap.
[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 runs much faster than simple nested loops.
Understanding Heap Sort's time complexity shows you can analyze complex algorithms and explain why they are efficient, a key skill in coding interviews.
"What if we used a simple array instead of a heap for sorting? How would the time complexity change?"
Practice
heapify function in the Heap Sort algorithm?Solution
Step 1: Understand heapify's role
The heapify function rearranges elements to satisfy the max heap property, where each parent node is greater than its children.Step 2: Connect heapify to building max heap
Heapify is called repeatedly to build a max heap from an unordered array before sorting begins.Final Answer:
To build a max heap from an unordered array -> Option AQuick Check:
heapify builds max heap = A [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: Identify correct C# swap syntax
In C#, swapping requires a temporary variable to hold one value during exchange.Step 2: Check each option
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; uses a temporary variable correctly. arr[i], arr[j] = arr[j], arr[i]; is Python syntax. swap(arr[i], arr[j]); is invalid without a swap method. arr[i] = arr[j]; arr[j] = arr[i]; overwrites values incorrectly.Final Answer:
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option BQuick Check:
Use temp variable to swap in C# = C [OK]
- Using Python tuple swap syntax in C#
- Calling undefined swap function
- Overwriting values without temp variable
int[] arr = {4, 10, 3, 5, 1};?Solution
Step 1: Build max heap from array
Starting array: [4, 10, 3, 5, 1]. Max heap after heapify: [10, 5, 3, 4, 1].Step 2: Extract max and sort
Swap 10 with last element, heapify reduced heap: [5, 4, 3, 1, 10]. Repeat until sorted: final sorted array is [1, 3, 4, 5, 10].Final Answer:
[1, 3, 4, 5, 10] -> Option DQuick Check:
Heap sort sorts 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 HeapSort(int[] arr) {
int n = arr.Length;
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, n, 0);
}
}
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 swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
Heapify(arr, n, largest);
}
}Solution
Step 1: Examine the second loop's Heapify call
After swapping arr[0] and arr[i], the effective heap size is i. However, the code calls Heapify(arr, n, 0), using the original full size n instead of the reduced size i. This is the error.Step 2: Confirm other options are not errors
The first for loop should start from n/2, not n/2 - 1 is incorrect because it correctly starts at n/2 - 1. The second for loop condition should be i >= 0, not i > 0 is wrong because i > 0 is appropriate. Swap logic inside Heapify is incorrect is false; the swap is correct.Final Answer:
Heapify call inside second loop uses wrong size parameter -> Option AQuick Check:
Heapify in extract phase uses reduced size i, not n = B [OK]
- Changing loop start incorrectly
- Using wrong loop end condition
- Misunderstanding heap size in heapify
Solution
Step 1: Understand stability in sorting
Stability means equal elements keep their original order after sorting.Step 2: Analyze heap sort stability
Standard heap sort is not stable because heap operations can reorder equal elements arbitrarily.Step 3: Find modification to improve stability
Adding an index field and comparing indices when values are equal preserves original order, making heap sort stable.Final Answer:
Add an index field to elements and compare indices when values are equal -> Option CQuick Check:
Use index to preserve order for stability = D [OK]
- Thinking min heap makes sort stable
- Skipping heapify breaks heap property
- Sorting twice wastes time without stability
Ready to prove it?
Pass the test and this pattern moves unknown
