Skip to content

Heap sort

ordering

Level 3 of 3

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

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.

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.

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.

question 4

Which is true of heap sort's comparisons on nn numbers, from 4 numbers on?

the answer, and why

the answer is D

Every sift makes at most hh rounds of 2 comparisons, which section 4 adds up.

this tests where the formula comes from, on the learn page

if you chose another

  • On equal numbers it costs 3n−63n - 6, which is Θ(n)\Theta(n).
  • From 4 numbers on, equal numbers cost less.
  • Equal numbers cost the least.

pen and paper

question 5about 8 minutes

Heap sort works on seven equal numbers, 5, 5, 5, 5, 5, 5, 5.

  1. Count the comparisons that build the heap.
  2. Count those that sort it.
  3. Check the total against 3n−63n - 6.
a hint

Every sift stops after one round.

a worked answer

Building: places 3, 2 and 1, 2 comparisons each, 6. Heap of 6: 2. Heap of 5: 2. Heap of 4: 2. Heap of 3: 2. Heap of 2: 1. Sorting makes 9. In all 15, and 3 times 7, less 6, is 15.

Why do nn equal numbers cost exactly 3n−63n - 6 comparisons, the fewest any row can?
  1. the move: one round each

    the line:
    a sift compares the childrenof its place, then stops\begin{gathered} \text{a sift compares the children} \\ \text{of its place, then stops} \end{gathered}
    siftplaceits childrencomparisons
    building24 and 52
    building12 and 32
    heap of 412 and 32
    heap of 312 and 32
    heap of 2121
    in all9
    Heap sort on 8, 8, 8, 8, 8, sift by sift. 4 comparisons build the heap, and 5 sort it.

    why it is allowed: With equal numbers no child is bigger, so every sift stops after its first round. The picture counts 8, 8, 8, 8, 8.

  2. the move: count the building

    the line:
    building compareseach child once\begin{gathered} \text{building compares} \\ \text{each child once} \end{gathered}

    why it is allowed: Each place with a child is sifted once, and every place but place 1 is the child of one place. So n−1n - 1 comparisons; 4 for 5 numbers.

  3. the move: count the sorting

    the line:
    sorting compares2(n−3)+1\begin{gathered} \text{sorting compares} \\ 2(n - 3) + 1 \end{gathered}

    why it is allowed: The tops of heaps of n−1n - 1 numbers down to 2 are sifted. The n−3n - 3 heaps of 3 or more give their top 2 children, as the heaps of 4 and 3 in the picture. The heap of 2 gives 1.

  4. the move: add them

    the line:
    B=(n−1)+2(n−3)+1B = (n - 1) + 2(n - 3) + 1

    why it is allowed: BB is the comparisons on equal numbers, by lines 2 and 3.

  5. the move: multiply out

    the line:
    B=(n−1)+(2n−6)+1B = (n - 1) + (2n - 6) + 1

    why it is allowed: Twice n−3n - 3 is 2n−62n - 6.

  6. the move: gather

    the line:
    B=3n−6B = 3n - 6

    why it is allowed: The 1s cancel. For 5 numbers, 4 + 4 + 1 is 9, as in the picture.

  7. the move: no row costs fewer

    the line:
    every row comparesat least this\begin{gathered} \text{every row compares} \\ \text{at least this} \end{gathered}

    why it is allowed: On any row, every sift compares at least the children of its place. Lines 2 and 3 count only those.

  8. the move: a ceiling

    the line:
    B≤3nB \le 3n

    why it is allowed: By line 6, taking 6 away.

  9. the move: read it as a bound

    the line:
    B∈O(n)B \in O(n)

    why it is allowed: By line 8, BB stays under 3 times nn.

  10. the move: a floor

    the line:
    B≥n for n≥3B \ge n \text{ for } n \ge 3

    why it is allowed: 3n−63n - 6 is nn plus 2n−62n - 6, and 2n−62n - 6 is at least 0 from 3 on. At 5, 9 is 5 + 4.

  11. the move: read it as a bound

    the line:
    B∈Ω(n)B \in \Omega(n)

    why it is allowed: By line 10.

  12. the move: ceiling and floor together

    the line:
    B∈Θ(n)B \in \Theta(n)

    why it is allowed: Lines 9 and 11 bound BB by the same growth, nn.

try it with a small nn: at n=5n = 5, line 4 gives B=9B = 9, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 6 to build.
  • 9 to sort.
  • 15, matching the formula.

question 6about 8 minutes

Section 1 sorts 42, 7, 31, 18, 56, 23, 12 with 19 comparisons.

  1. Work out UU at 7, the count no row of 7 numbers goes past.
  2. Work out FF at 7, the floor under the dearest row.
  3. Put 19 and the dearest row of 7, which costs 22, in order between them.
a hint

At 7, the height hh is 2 and there are 3 parents. For FF, find the first power of 2 at or above 5,040, the orders of 7 numbers.

a worked answer

UU is 2 times 2 times (3 + 7 - 2), so 4 times 8, which is 32. 2 to the 12 is 4,096, below 5,040, and 2 to the 13 is 8,192, so FF is 13. In order: 13, then this row's 19, then the dearest row's 22, then 32. The dearest row sits between its floor and the ceiling, and this row costs less.

this tests where the formula comes from, on the learn page

what a full answer includes
  • UU is 32.
  • FF is 13.
  • 13, 19, 22, 32.