0
0
DSA Goprogramming~10 mins

Heap Sort Algorithm in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Heap Sort Algorithm
Build Max Heap
Heapify from last parent to root
Swap root with last element
Reduce heap size by 1
Heapify root again
Repeat swap and heapify until heap size is 1
Sorted array ready
Heap sort first builds a max heap, then repeatedly swaps the root with the last element and heapifies the reduced heap until sorted.
Execution Sample
DSA Go
arr := []int{4, 10, 3, 5, 1}
heapSort(arr)
// Output sorted array
Sorts the array [4, 10, 3, 5, 1] using heap sort algorithm.
Execution Table
StepOperationHeap SizeArray StateHeap Visual (Tree)Notes
1Build max heap: heapify index 15[4, 10, 3, 5, 1] 4 / \ 10 3 / \ 5 1Heapify at index 1: children 5 and 1, max child 5 > 10? No, no swap
2Build max heap: heapify index 05[10, 5, 3, 4, 1] 10 / \ 5 3 / \ 4 1Swap 4 and 10, then heapify subtree at index 1
3Heapify index 1 after swap5[10, 5, 3, 4, 1] 10 / \ 5 3 / \ 4 1No further swaps needed
4Swap root with last element (index 4)5[1, 5, 3, 4, 10] 1 / \ 5 3 / \ 4 10Swap 10 and 1, reduce heap size to 4
5Heapify root (index 0) with heap size 44[5, 4, 3, 1, 10] 5 / \ 4 3 / \ 1 10Swap 1 and 5, then heapify index 1
6Heapify index 14[5, 4, 3, 1, 10] 5 / \ 4 3 / \ 1 10No swap needed
7Swap root with last element (index 3)4[1, 4, 3, 5, 10] 1 / \ 4 3 / \ 5 10Swap 5 and 1, reduce heap size to 3
8Heapify root (index 0) with heap size 33[4, 1, 3, 5, 10] 4 / \ 1 3 / \ 5 10Swap 1 and 4, then heapify index 1
9Heapify index 13[4, 1, 3, 5, 10] 4 / \ 1 3 / \ 5 10No swap needed
10Swap root with last element (index 2)3[3, 1, 4, 5, 10] 3 / \ 1 4 / \ 5 10Swap 4 and 3, reduce heap size to 2
11Heapify root (index 0) with heap size 22[3, 1, 4, 5, 10] 3 / \ 1 4 / \ 5 10No swap needed
12Swap root with last element (index 1)2[1, 3, 4, 5, 10] 1 / \ 3 4 / \ 5 10Swap 3 and 1, reduce heap size to 1
13Heapify root (index 0) with heap size 11[1, 3, 4, 5, 10] 1 / \ 3 4 / \ 5 10Heap size 1, no heapify needed
14Sorting complete1[1, 3, 4, 5, 10] 1 / \ 3 4 / \ 5 10Array sorted in ascending order
💡 Heap size reduced to 1, array fully sorted
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7After Step 10After Step 12Final
arr[4,10,3,5,1][10,5,3,4,1][1,5,3,4,10][1,4,3,5,10][3,1,4,5,10][1,3,4,5,10][1,3,4,5,10]
heapSize5544321
rootIndexN/A00000N/A
Key Moments - 3 Insights
Why do we start heapifying from the last parent node and not from the root?
Because leaf nodes are already heaps. Starting from the last parent ensures all subtrees become max heaps, as shown in steps 1 and 2 where heapify begins at index 1 then index 0.
Why do we swap the root with the last element and then reduce heap size?
The root is the largest element in max heap. Swapping it to the end places it in its correct sorted position. Reducing heap size excludes it from further heapify, as seen in steps 4, 7, 10, and 12.
Why do we heapify the root again after each swap?
Swapping breaks the heap property at root. Heapifying restores max heap structure for the reduced heap, as shown in steps 5, 8, 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the heap size after swapping the root with the last element?
A5
B4
C3
D1
💡 Hint
Check the 'Heap Size' column at step 4 in the execution table.
At which step does the array first become a valid max heap?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Operation' and 'Array State' columns in steps 1 to 3.
If we skip heapifying the root after swapping, what would happen to the array?
AHeap property would break, array not sorted correctly
BIt would become a min heap
CIt would remain sorted
DHeap size would increase
💡 Hint
Refer to steps 5, 8, and 11 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 arranging the array so the largest value is at the root. We do this by heapifying from the last parent node up to the root. Then, we swap the root with the last element in the heap and reduce the heap size by one, effectively placing the largest element at the end of the array. After each swap, we heapify the root again to maintain the max heap property for the remaining elements. We repeat this process until the heap size is one, at which point the array is fully sorted in ascending order. The execution table shows each step with the array state and heap visual, helping to understand how the heap changes during sorting.