0
0
DSA Javascriptprogramming~10 mins

Heap Sort Algorithm in DSA Javascript - 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
Heapify root
Repeat swap and heapify until heap size is 1
Sorted Array
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 Javascript
function heapSort(arr) {
  buildMaxHeap(arr);
  for (let end = arr.length - 1; end > 0; end--) {
    [arr[0], arr[end]] = [arr[end], arr[0]];
    heapify(arr, 0, end);
  }
}
This code sorts an array by building a max heap and then sorting by swapping and heapifying.
Execution Table
StepOperationHeap SizeArray StateAction/Pointer ChangesVisual State
1Build Max Heap start6[4, 10, 3, 5, 1, 2]Heapify from index 2 to 0Initial array as heap
2Heapify index 26[4, 10, 3, 5, 1, 2]No change, 3 > childrenHeap unchanged
3Heapify index 16[4, 10, 3, 5, 1, 2]No swap, 10 > 5 and 1Heap unchanged
4Heapify index 06[10, 4, 3, 5, 1, 2]Swap 4 and 10Array: [10, 4, 3, 5, 1, 2]
5Heapify index 16[10, 5, 3, 4, 1, 2]Swap 4 and 5Array: [10, 5, 3, 4, 1, 2]
6Max Heap built6[10, 5, 3, 4, 1, 2]Heap readyMax heap complete
7Swap root and end6[2, 5, 3, 4, 1, 10]Swap 10 and 2, reduce size to 5Array: [2, 5, 3, 4, 1, 10]
8Heapify root5[5, 2, 3, 4, 1, 10]Swap 2 and 5Array: [5, 2, 3, 4, 1, 10]
9Heapify index 15[5, 4, 3, 2, 1, 10]Swap 2 and 4Array: [5, 4, 3, 2, 1, 10]
10Swap root and end5[1, 4, 3, 2, 5, 10]Swap 5 and 1, reduce size to 4Array: [1, 4, 3, 2, 5, 10]
11Heapify root4[4, 1, 3, 2, 5, 10]Swap 1 and 4Array: [4, 1, 3, 2, 5, 10]
12Heapify index 14[4, 2, 3, 1, 5, 10]Swap 1 and 2Array: [4, 2, 3, 1, 5, 10]
13Swap root and end4[1, 2, 3, 4, 5, 10]Swap 4 and 1, reduce size to 3Array: [1, 2, 3, 4, 5, 10]
14Heapify root3[3, 2, 1, 4, 5, 10]Swap 1 and 3Array: [3, 2, 1, 4, 5, 10]
15Swap root and end3[1, 2, 3, 4, 5, 10]Swap 3 and 1, reduce size to 2Array: [1, 2, 3, 4, 5, 10]
16Heapify root2[2, 1, 3, 4, 5, 10]Swap 1 and 2Array: [2, 1, 3, 4, 5, 10]
17Swap root and end2[1, 2, 3, 4, 5, 10]Swap 2 and 1, reduce size to 1Array: [1, 2, 3, 4, 5, 10]
18Heapify root1[1, 2, 3, 4, 5, 10]No heapify neededHeap size 1, done
19Sorting complete1[1, 2, 3, 4, 5, 10]All sortedFinal sorted array
💡 Heap size reduced to 1, array fully sorted
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 7After Step 10After Step 13After Step 15Final
arr[4, 10, 3, 5, 1, 2][10, 4, 3, 5, 1, 2][10, 5, 3, 4, 1, 2][2, 5, 3, 4, 1, 10][1, 4, 3, 2, 5, 10][1, 2, 3, 4, 5, 10][1, 2, 3, 4, 5, 10][1, 2, 3, 4, 5, 10]
heapSize66654321
root00000000
Key Moments - 3 Insights
Why do we heapify from the last parent node up to the root when building the max heap?
Heapify from the last parent ensures all subtrees satisfy max heap property before reaching the root, as shown in steps 2 to 5 in the execution_table.
Why do we swap the root with the last element before heapifying again?
Swapping moves the largest element (root) to its correct sorted position at the end, then heapify fixes the heap for the remaining elements, as seen in steps 7 and 8.
Why does the heap size reduce after each swap?
Because the last element is sorted and excluded from the heap, reducing heap size ensures heapify only affects unsorted elements, shown in the heapSize changes in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the array state after swapping the root with the last element?
A[10, 5, 3, 4, 1, 2]
B[2, 5, 3, 4, 1, 10]
C[1, 4, 3, 2, 5, 10]
D[5, 4, 3, 2, 1, 10]
💡 Hint
Check the 'Array State' column at step 7 in the execution_table.
At which step does the heap size reduce from 6 to 5?
AStep 7
BStep 6
CStep 8
DStep 9
💡 Hint
Look at the 'Heap Size' column in the execution_table and variable_tracker.
If we skip heapifying after swapping the root with the last element, what would happen to the array?
AThe array would remain sorted
BThe array would become reversed
CThe max heap property would be violated, causing incorrect sorting
DNothing changes, heapify is optional
💡 Hint
Refer to the 'Action/Pointer Changes' and 'Visual State' columns after swaps in the execution_table.
Concept Snapshot
Heap Sort Algorithm:
- Build a max heap from the array
- Swap root (max) with last element
- Reduce heap size by 1
- Heapify root to restore max heap
- Repeat swap and heapify 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 that each parent node is larger than its children. We do this by heapifying from the last parent node up to the root. Once the max heap is built, the largest element is at the root (index 0). We swap this root with the last element in the heap, effectively placing the largest element at its correct sorted position. Then we reduce the heap size by one, excluding the sorted element, and heapify the root again to restore the max heap property. We repeat this process of swapping and heapifying until the heap size is 1, meaning the array is fully sorted. The execution table shows each step with the array state and heap size changes. The variable tracker follows the array and heap size over time. Key moments clarify why heapify starts from the last parent, why swapping is necessary, and why heap size reduces. The visual quiz tests understanding of array states and heap size changes during sorting.