Heap Sort: Step-by-Step Process
Given Array: [5, 8, 3, 9, 2, 10, 1, 45, 32]
Step 1: Build Max Heap
Resulting Max Heap: [45, 8, 10, 5, 2, 3, 1, 9, 32]
Step 2: Sorting Process
Swap root with last element:
[32, 8, 10, 5, 2, 3, 1, 9, 45] - Heapify
Swap root with second last element:
[10, 8, 32, 5, 2, 3, 1, 9, 45] - Heapify
Swap root with third last element:
[9, 8, 1, 5, 2, 3, 32, 10, 45] - Heapify
Swap root with fourth last element:
[3, 8, 1, 5, 2, 9, 32, 10, 45] - Heapify
Swap root with fifth last element:
[2, 5, 1, 3, 8, 9, 32, 10, 45] - Heapify
Swap root with sixth last element:
[2, 3, 1, 5, 8, 9, 32, 10, 45] - Heapify
Swap root with seventh last element:
[1, 2, 3, 5, 8, 9, 32, 10, 45] - Heapify
Final swap:
[1, 2, 3, 5, 8, 9, 10, 32, 45] - Sorted Array
Final Sorted Array: [1, 2, 3, 5, 8, 9, 10, 32, 45]