0
0
DSA C++programming~10 mins

Heap Sort Algorithm in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Heap Sort Algorithm
Build Max Heap from array
Extract max element (root)
Swap root with last element
Reduce heap size by 1
Heapify root to restore max heap
Repeat extraction until heap size is 1
Sorted array obtained
Heap sort first builds a max heap from the array, then repeatedly extracts the largest element by swapping the root with the last element and heapifying the root, shrinking the heap each time until sorted.
Execution Sample
DSA C++
void heapSort(int arr[], int n) {
  buildMaxHeap(arr, n);
  for (int i = n - 1; i > 0; i--) {
    swap(arr[0], arr[i]);
    heapify(arr, i, 0);
  }
}
This code builds a max heap and then sorts the array by extracting the max repeatedly.
Execution Table
StepOperationHeap SizeArray StateHeap Visual (Tree)Action Details
1Build Max Heap6[4, 10, 3, 5, 1, 2] 4 / \ 10 3 / \ / 5 1 2Heapify from bottom non-leaf nodes to root
2Heapify at index 26[4, 10, 3, 5, 1, 2] 4 / \ 10 3 / \ / 5 1 2No change, 3 is larger than children
3Heapify at index 16[4, 10, 3, 5, 1, 2] 4 / \ 10 3 / \ / 5 1 2Swap 10 with 5, then heapify at child
4Heapify at index 36[4, 10, 3, 5, 1, 2] 4 / \ 10 3 / \ / 5 1 2Leaf node, no change
5Heapify at index 06[10, 5, 3, 4, 1, 2] 10 / \ 5 3 / \ / 4 1 2Swap 4 and 10, heapify children
6Extract max and swap with last6[2, 5, 3, 4, 1, 10] 2 / \ 5 3 / \ / 4 1 10Swap root 10 with last element 2
7Heapify root (index 0)5[5, 4, 3, 2, 1, 10] 5 / \ 4 3 / \ 2 1Swap 2 and 5, heapify children
8Extract max and swap with last5[1, 4, 3, 2, 5, 10] 1 / \ 4 3 / \ 2 5 10Swap root 5 with last element 1
9Heapify root (index 0)4[4, 2, 3, 1, 5, 10] 4 / \ 2 3 / \ 1 5 10Swap 1 and 4, heapify children
10Extract max and swap with last4[1, 2, 3, 4, 5, 10] 1 / \ 2 3 / \ 4 5 10Swap root 4 with last element 1
11Heapify root (index 0)3[3, 2, 1, 4, 5, 10] 3 / \ 2 1Swap 1 and 3, heapify children
12Extract max and swap with last3[1, 2, 3, 4, 5, 10] 1 / \ 2 3Swap root 3 with last element 1
13Heapify root (index 0)2[2, 1, 3, 4, 5, 10] 2 / \ 1Swap 1 and 2
14Extract max and swap with last2[1, 2, 3, 4, 5, 10] 1 / \ 2Swap root 2 with last element 1
15Heapify root (index 0)1[1, 2, 3, 4, 5, 10] 1Only one element, no heapify needed
16Sorting complete1[1, 2, 3, 4, 5, 10] 1Heap size 1, array sorted
💡 Heap size reduced to 1, array fully sorted
Variable Tracker
VariableStartAfter Step 5After Step 7After Step 9After Step 11After Step 13Final
Heap Size6654321
Array[4,10,3,5,1,2][10,5,3,4,1,2][5,4,3,2,1,10][4,2,3,1,5,10][3,2,1,4,5,10][2,1,3,4,5,10][1,2,3,4,5,10]
Root Value41054321
Key Moments - 3 Insights
Why do we build the max heap starting from the bottom non-leaf nodes?
Because leaf nodes are already heaps of size 1, starting from bottom non-leaf nodes ensures heap property is fixed from bottom up, as shown in steps 2-5 in the execution_table.
Why do we swap the root with the last element before heapifying?
Swapping moves the largest element to its correct sorted position at the end, then heapify restores the heap property for the reduced heap, as seen in steps 6 and 7.
Why does heap size reduce after each extraction?
Because the last element is fixed in sorted position and excluded from the heap, reducing heap size ensures heapify only works on the unsorted part, shown in variable_tracker and steps 7, 9, 11, etc.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the root value after swapping?
A4
B10
C2
D5
💡 Hint
Check the 'Array State' and 'Heap Visual' columns at step 6 in execution_table.
At which step does the heap size become 3?
AStep 9
BStep 11
CStep 13
DStep 15
💡 Hint
Refer to the 'Heap Size' column in execution_table rows.
If we skip heapifying after swapping root with last element, what happens?
AHeap property breaks, sorting fails
BArray remains sorted
CHeap size increases
DRoot value stays the same
💡 Hint
Look at the 'Action Details' column in execution_table where heapify restores heap property.
Concept Snapshot
Heap Sort Algorithm:
- Build max heap from array
- Swap root (max) with last element
- Reduce heap size by 1
- Heapify root to restore max heap
- Repeat until heap size is 1
- Result: sorted array in ascending order
Full Transcript
Heap sort works by first building a max heap from the input array. This means the largest element is at the root. Then, the root is swapped with the last element in the heap, effectively placing the largest element at the end of the array. The heap size is reduced by one to exclude the sorted element. Next, the heap property is restored by heapifying the root. This process repeats until only one element remains in the heap, resulting in a fully sorted array. The execution table shows each step with the array state and heap structure. The variable tracker follows heap size and array changes. Key moments clarify why heapify is done bottom-up and why heap size reduces. The visual quiz tests understanding of root values, heap size changes, and the importance of heapify. The concept snapshot summarizes the algorithm steps clearly.