0
0
Data Structures Theoryknowledge~10 mins

Heap sort algorithm in Data Structures Theory - Step-by-Step Execution

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 heap
Repeat extraction until heap empty
Sorted array
Heap sort first builds a max heap, then repeatedly extracts the largest element by swapping it with the last heap element and restoring the heap, until the array is sorted.
Execution Sample
Data Structures Theory
array = [4, 10, 3, 5, 1]
build_max_heap(array)
for i in range(len(array)-1, 0, -1):
  swap(array[0], array[i])
  heapify(array, 0, i)
This code builds a max heap from the array, then sorts it by swapping the root with the last element and heapifying the reduced heap repeatedly.
Analysis Table
StepOperationArray StateHeap SizeAction Description
1Build max heap[4, 10, 3, 5, 1]5Heapify from bottom non-leaf nodes to root
2Heapify at index 1[4, 10, 3, 5, 1]5No change, subtree already max heap
3Heapify at index 0[10, 5, 3, 4, 1]5Swap 4 and 10 to fix heap
4Swap root with last[1, 5, 3, 4, 10]5Move max 10 to end
5Heapify root[5, 4, 3, 1, 10]4Restore heap property in reduced heap
6Swap root with last[1, 4, 3, 5, 10]4Move max 5 to sorted position
7Heapify root[4, 1, 3, 5, 10]3Restore heap property
8Swap root with last[3, 1, 4, 5, 10]3Move max 4 to sorted position
9Heapify root[3, 1, 4, 5, 10]2Restore heap property
10Swap root with last[1, 3, 4, 5, 10]2Move max 3 to sorted position
11Heapify root[1, 3, 4, 5, 10]1Heap size 1, no heapify needed
12Swap root with last[1, 3, 4, 5, 10]1Only one element left, sorting done
13End[1, 3, 4, 5, 10]0Heap empty, array sorted ascending
💡 Heap size reduced to 0, all elements sorted
State Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9Final
array[4, 10, 3, 5, 1][10, 5, 3, 4, 1][5, 4, 3, 1, 10][4, 1, 3, 5, 10][3, 1, 4, 5, 10][1, 3, 4, 5, 10]
heap_size554320
Key Insights - 3 Insights
Why do we build the max heap starting from the bottom non-leaf nodes?
Because leaf nodes are already heaps, starting from bottom non-leaf nodes ensures subtrees become max heaps before fixing higher nodes, as shown in steps 2 and 3.
Why do we swap the root with the last element before heapifying?
Swapping moves the largest element (root) to its correct sorted position at the end, then heapify restores the heap for the remaining elements, as seen in steps 4 and 5.
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 unsorted part, shown in variable 'heap_size' changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What change happens to the array?
ANo change, heap is already valid
BThe last element is removed
CThe root 4 is swapped with 10 to fix the heap
DThe array is sorted
💡 Hint
Check 'Array State' and 'Action Description' columns at step 3
At which step does the heap size reduce from 5 to 4?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at 'Heap Size' column changes between steps 4 and 5
If we skip heapifying after swapping root with last element, what happens?
AThe heap property breaks, sorting fails
BThe array remains sorted
CHeap size increases
DNothing changes
💡 Hint
Refer to 'Action Description' for heapify steps after swaps in execution table
Concept Snapshot
Heap sort builds a max heap from the array.
Then repeatedly swaps the root (max) with the last element.
Reduces heap size and heapifies root to restore heap.
Repeats until heap is empty.
Result is a sorted array in ascending order.
Full Transcript
Heap sort algorithm first transforms the input array into a max heap by heapifying from bottom non-leaf nodes up to the root. This ensures the largest element is at the root. Then, it repeatedly swaps the root element with the last element in the heap, effectively moving the largest element to its correct sorted position. After each swap, the heap size is reduced by one, excluding the sorted elements, and the heap property is restored by heapifying the root. This process continues until the heap size is zero, resulting in a fully sorted array in ascending order. Key points include building the heap from bottom up, swapping root with last element before heapifying, and reducing heap size after each extraction.