In first Example, first we have to build Max Heap.
So, we will start from 20 as child and check for its parent. Here 10 is smaller, so we will swap these two.
Now, 20 10 15 17 9 21
Now, child 17 is greater than its parent 10. So, both will be swapped and order will be 20 17 15 10 9 21
Now, child 21 is greater than parent 15. So, both will be swapped.
20 17 21 10 9 15
Now, again 21 is bigger than parent 20. So, 21 17 20 10 9 15
This is Max Heap.
Now, we have to apply sorting. Here, we have to swap first element with last one and we have to maintain Max Heap property. So, after first swapping : 15 17 20 10 9 21 It clearly violates Max Heap property.
So, we have to maintain it. So, order will be
20 17 15 10 9 21
17 10 15 9 20 21
15 10 9 17 20 21
10 9 15 17 20 21
9 10 15 17 20 21
Here, underlined part is sorted part.