Skip to content

Heap sort

ordering

Level 3 of 3

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from heap sort's own run on the numbers shown.

choose one

question 1

Heap sort starts on 15, 40, 22, 9, 31. What is the row once the heap is built, before the first swap of the top?

15402293111524032249531
the answer, and why

the answer is C

Place 2 sifts first, and 40 is bigger than its children. Then 15 at the top swaps with 40, then with 31.

if you chose another

  • That stops the sift of 15 one round early. 31 is bigger, so 15 goes down again.
  • That is the end of the sort.
  • That is the start. 15 is smaller than its child 40.

new numbers

question 2

How many comparisons does heap sort make on 6, 6, 6, 6, 6, 6?

666666162636465666
the answer, and why

the answer is C

Building compares each child once, 5. Sorting compares the tops' children, 2 each for heaps of 5, 4 and 3, then 1. That is 5 + 7 = 12.

if you chose another

  • That is the building only.
  • That is the sorting only.
  • That is 3 times 6, but the formula takes 6 away.

new numbers

question 3

On which row does heap sort make the fewest comparisons?

the answer, and why

the answer is A

Equal numbers make every sift stop at once, 12 comparisons. The others cost 16, 14 and 16.

if you chose another

  • In order, the small numbers sit on top and sift down far.
  • Back to front is already a heap, but each new top still sifts down.
  • A shuffle sifts down too.

new numbers