CMPS 144 Intersession 2020
Lab #10: HeapSort

Consider the following array.

   0    1    2    3    4    5    6    7    8    9   10 
+----+----+----+----+----+----+----+----+----+----+----+
|  7 | 12 |  5 | 27 |  2 | -8 | 17 | 15 |  4 |  9 | 13 |
+----+----+----+----+----+----+----+----+----+----+----+

Viewed as a complete binary tree, the array looks like this:

                 7
                / \
               /   \
              /     \
             /       \
            /         \
           /           \
         12             5
        /  \           / \
       /    \         /   \
      /      \       /     \
     /        \     /       \
   27          2   -8       17
  /  \        / \
 /    \      /   \
15     4    9    13

Apply the HeapSort algorithm to the array and show what the array/tree looks like at particular points in time. Specifically:

1. Phase 1 of HeapSort applies siftDown to each non-leaf node in the tree. Show what the array/tree looks like after each such application. (Your answer should include five trees.)

2. Each iteration of Phase 2 of HeapSort swaps the values in the root node and the last node that is still part of the max-heap built during Phase 1. (That last node is then no longer part of the max-heap.) Then it applies siftDown to the root. Show what the array/tree looks like after each of the ten iterations that are carried out during Phase 2.