Step 1: Start heapify from last parent node at index 2 (value 30)
[15, 10, 30, 50, 20, 8, 5]
Why: We start from last parent to fix heap property bottom-up
Step 2: Compare 30 with children 8 and 5; 30 is largest, no change
[15, 10, 30, 50, 20, 8, 5]
Why: No need to swap if parent is already largest
Step 3: Move to parent at index 1 (value 10), compare with children 50 and 20
[15, 10, 30, 50, 20, 8, 5]
Why: Check if parent is smaller than children to maintain max heap
Step 4: Swap 10 with largest child 50
[15, 50, 30, 10, 20, 8, 5]
Why: Parent must be larger, so swap with largest child
Step 5: Heapify subtree rooted at index 3 (value 10), children none
[15, 50, 30, 10, 20, 8, 5]
Why: No children to compare, subtree heap property satisfied
Step 6: Move to root at index 0 (value 15), compare with children 50 and 30
[15, 50, 30, 10, 20, 8, 5]
Why: Root must be largest to maintain max heap
Step 7: Swap 15 with largest child 50
[50, 15, 30, 10, 20, 8, 5]
Why: Swap to keep max heap property
Step 8: Heapify subtree rooted at index 1 (value 15), compare with children 10 and 20
[50, 15, 30, 10, 20, 8, 5]
Why: Fix heap property in subtree
Step 9: Swap 15 with largest child 20
[50, 20, 30, 10, 15, 8, 5]
Why: Parent must be larger, so swap
Step 10: Heapify subtree rooted at index 4 (value 15), no children
[50, 20, 30, 10, 15, 8, 5]
Why: No children, heap property satisfied
Result: Final max heap array: [50, 20, 30, 10, 15, 8, 5]
Heap structure:
[50]
/ \
[20] [30]
/ \ / \
[10] [15][8] [5]