Challenge - 5 Problems
Heap Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Heap Sort on a small array
What is the output of the following C# code after performing heap sort on the array?
DSA C#
using System; class Program { static 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); } } static 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, i, 0); } } static void Main() { int[] arr = {4, 10, 3, 5, 1}; HeapSort(arr); Console.WriteLine(string.Join(" -> ", arr) + " -> null"); } }
Attempts:
2 left
💡 Hint
Heap sort sorts the array in ascending order by repeatedly moving the largest element to the end.
✗ Incorrect
Heap sort builds a max heap, then swaps the root with the last element, reduces heap size, and heapifies again. The final array is sorted ascending.
🧠 Conceptual
intermediateHeap Sort Time Complexity
What is the time complexity of heap sort in the average and worst cases?
Attempts:
2 left
💡 Hint
Heap sort always builds a heap and performs heapify operations.
✗ Incorrect
Heap sort always takes O(n log n) time because building the heap is O(n) and each of the n removals takes O(log n).
🔧 Debug
advancedIdentify the error in heapify function
What error will occur when running this heapify function on an array?
DSA C#
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); } }
Attempts:
2 left
💡 Hint
Check the boundary conditions for left and right child indices.
✗ Incorrect
The conditions use 'left <= n' and 'right <= n' which allow index n, but valid indices are 0 to n-1. This causes out of range access.
❓ Predict Output
advancedHeap Sort on a reversed array
What is the output after heap sorting this reversed array?
DSA C#
using System; class Program { static 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); } } static 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, i, 0); } } static void Main() { int[] arr = {9, 7, 5, 3, 1}; HeapSort(arr); Console.WriteLine(string.Join(" -> ", arr) + " -> null"); } }
Attempts:
2 left
💡 Hint
Heap sort always sorts ascending regardless of initial order.
✗ Incorrect
Heap sort rearranges the reversed array into ascending order: 1, 3, 5, 7, 9.
🧠 Conceptual
expertHeap Sort Stability
Is heap sort a stable sorting algorithm? Why or why not?
Attempts:
2 left
💡 Hint
Think about what happens when two equal elements are swapped during heapify.
✗ Incorrect
Heap sort is not stable because it swaps elements during heapify that can reorder equal elements.
