Step 1: Start heapify from last parent node at index 2 (value 30)
[15, 10, 30, 50, 20, 8, 5]
Why: We begin from last parent to ensure subtrees below are heaps before fixing parents
Step 2: Compare 30 with children 8 and 5; 30 is largest, no swap
[15, 10, 30, 50, 20, 8, 5]
Why: No change needed as parent is already larger than children
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 heap property holds at this node
Step 4: Swap 10 with largest child 50
[15, 50, 30, 10, 20, 8, 5]
Why: Parent must be larger than children in max heap
Step 5: Heapify subtree rooted at index 3 (value 10), compare with children none (leaf)
[15, 50, 30, 10, 20, 8, 5]
Why: No children to compare, subtree is heap
Step 6: Move to root at index 0 (value 15), compare with children 50 and 30
[15, 50, 30, 10, 20, 8, 5]
Why: Check if root satisfies heap property
Step 7: Swap 15 with largest child 50
[50, 15, 30, 10, 20, 8, 5]
Why: Root must be largest in max heap
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 than children
Step 10: Heapify subtree rooted at index 4 (value 15), no children to compare
[50, 20, 30, 10, 15, 8, 5]
Why: Leaf node, heap property holds
Result: Final max heap array: [50, 20, 30, 10, 15, 8, 5]
Heap structure:
[50]
/ \
[20] [30]
/ \ / \
[10] [15] [8] [5]