Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MCS-031 - Question 1:

Ans: (iii) Heap Sort

Input to the heap algorithm

187,62,155,343,184,958,365,427,78,94,121,388

Step 1: (Inserting 187) Step 2: (Inserting 62)

187
187

62

Step 3: (Inserting 155) Here the values in the right side must be
greater than the root, but while inserting
187 155 it violates the heap rule, so the heap
must be adjusted as follows:
155
62 155

62 187

Step 4: (Inserting 343) Again the rule of heap is violated as 343


is greater than the root, it must be less
155 than the root to get inserted in the left
side. So we will now arrange the heap
as follows:

62 187

343

155 343 187

343 187 155 187 155 343

62 62 62
Step 5: (Inserting 184) Step 6: (Inserting 958)

187 187

155 343 155 343

62 184 62 184 958

187

Here also inserting 958 violates


the heap rule.
Now we will adjust them. 155 958

62 184 343

Step 7: (inserting 365) Need to adjust the heap.

187 187

155 958 155 365

62 184 343 365 62 184 343 958

Step 8: (Inserting 427) Adjusting the heap as follows:

187 187

155 365 155 365

62 184 343 958 427 184 343 958

427 62

Page 2 of 5
187 427

427 365 187 365

155 184 343 958 155 184 343 958

62 62
427 343

184 365 187 365

155 187 343 958 155 184 427 958

62 62

343 Step 9 (Inserting 78)

343
187 427
187 427

155 184 365 958


155 184 365 958
62

62 78

Now adjusting 78 as follows: Step 10: (Inserting 94)


343
343

187 427 187 427

78 184 365 958


78 184 365 958

62 155 94
62 155

Page 3 of 5
Adjusting 94 as follows:

343 343

94 427 94 427

78 184 365 958 78 187 365 958

62 155 187 62 155 184

Step 11: (Inserting 121)


343

343

155 427

155 427
78 187 365 958

78 187 365 958

62 94 184

62 94 184 121
Now adjusting 121 as follows:

343 343

155 427 121 427

78 121 365 958 78 155 365 958

62 94 184 187 62 94 184 187

Page 4 of 5
343

121 427

78 184 365 958

62 94 155 187

Step 12: (Inserting 388)


343

121 427

78 184 365 958

62 94 155 187 388

343 Now traversing the heap


in INORDER we get the
sorted list of the array.

121 427

78 184 388 958

62 94 155 187 365

Total No of Comparisons are 137.

Page 5 of 5

You might also like