Professional Documents
Culture Documents
____________________
Question 2a
You have to build Min heap for the given array elements along with their respective array positions;
Array = { 2,4,7,6,9,12,8,14,30}
Solution:
4 7
6 9 8 12
14 30
Question 2b
You have to perform Heap sort on the tree that you have built in Q.No.2a. Illustrate all steps of Heap
sort one-by-one in sequential way. Also show all elements in sorted order in an array.
Solution:
Step-1:
Remove smallest number
6 7
14 9 8 12
30
Array = { 2,4,7,6,9,12,8,14,30}
Step: 2:
6
14 7
30 9 8 12
Array = { 2,4,7,6,9,12,8,14,30}
Step- 3:
14 8
30 9 12
Array = { 2,4,7,6,9,12,8,14,30}
Step-4:
14 12
30 9
Array = { 2,4,7,6,9,12,8,14,30}
Step-5:
14 12
30
Array = { 2,4,7,6,9,12,8,14,30}
Step-6:
14 30
Array = { 2,4,7,6,9,12,8,14,30}
Step-7:
14
30
Array = { 2,4,7,6,9,12,8,14,30}
30
Array = { 2,4,7,6,9,12,8,14,30}
Step 9: Remove 30’s node
Array = { 2,4,7,6,9,12,8,14,30}
And hence Array is sorted
_______________________