Skip to content

Building a heap

storing

Level 2 of 3

Also called Heapify, Floyd's method and Bottom-up heap construction.

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

A row holds 10 numbers. Which places does building a heap sift, and in what order?

the answer, and why

the answer is C

The last parent is place 5, half of 10, with place 10 its only child. The parents sift from the last back to the first, so each finds both parts below it heaps already.

1234567891012345678910
A row of 10. The marked places 1 to 5 have children; places 6 to 10 have none. They sift 5 first, then 4, 3, 2 and 1.

this tests how it works, on the learn page

if you chose another

  • That sifts from the top down, before the parts below place 1 are heaps.
  • Places 6 to 10 have no children, as twice 6 is 12, past the end. Each is a heap of one already.
  • Those are the places with no children, which never sift.

question 2

On which of these rows does building a heap make no swap at all?

the answer, and why

the answer is A

Every parent is at least as big as its children, 91 over 58 and 73, 58 over 12 and 44, 73 over 60 and 29. So every sift stops at once.

91587312446029911582733124445606297
91, 58, 73, 12, 44, 60, 29 as a tree. Each marked parent is at least as big as its children.

if you chose another

  • 60 at place 5 is bigger than its parent, 58, so 58 swaps down.
  • 91 at place 2 is bigger than the top, 58, so 58 swaps down.
  • 60 at place 6 is bigger than its parent, 58, so 58 swaps down.

question 3

Each row holds the same six numbers. On which does building a heap make the most comparisons?

the answer, and why

the answer is A

27 at the top swaps with 62, then sinks into place 2, which has two children, for another round of 2. That makes 7. The others cost 6, 5 and 6.

sift at placeABCD
31111
22222
14323
in all7656
Each row's comparisons, sift by sift. Place 3 has one child.

if you chose another

  • Smallest first swaps the most, but costs 6. 15 sinks into place 3, which has one child, so its second round compares once.
  • That is a heap already, the cheapest, 5.
  • 59 sinks into place 3, which has one child, so its second round compares once. 6 in all.

question 4

Shuffled rows of 1,000 numbers cost about 1,870 comparisons each, on average. About how many does a shuffled row of a million cost?

the answer, and why

the answer is B

From 1,000 on, section 2 of the learn page shows the comparisons growing in step with the numbers, a thousand times as many for a thousand times the numbers. The measured mean at a million is 1,881,312.5.

this tests how it scales, on the learn page

if you chose another

  • That multiplies by a million, as if the cost grew with the square of the numbers.
  • That grows like n log⁡2 nn \,\log_2\, n. log⁡2 n\log_2\, n is about 10 at 1,000 and 20 at a million, so 1,870 × 1,000 × 2, about 3.7 million.
  • That multiplies by 2 only, 1,870 × 2, as if the cost grew like log⁡2 n\log_2\, n. Each of places 2 to a million is met once by its parent, one comparison each, so at least 999,999.

pen and paper

question 5about 12 minutes

1,023 numbers arrive at once, smallest first. They fill a full tree of 10 levels, 1, 2, 4 and on up to 512.

  1. Put them in one at a time. How many comparisons does that make?
  2. Build bottom up instead. At most how many comparisons, by line 6 of the question on the ceiling?
  3. Compare the two per number. Which way do you choose here?
a hint

One at a time, each number put on level dd climbs dd levels to the top. Bottom up, halve 1,023 again and again, rounding down.

a worked answer

Level dd holds 2 to the power dd numbers, each climbing dd. The grid adds the levels: 8,194. Bottom up, line 6 adds 511, 255, 127, 63, 31, 15, 7, 3 and 1, which make 1,013, and doubles it: 2,026. On a full heap a smallest-first row makes exactly that, as line 3 of the question on the ceiling shows. Per number, 8,194 over 1,023 is about 8, and 2,026 over 1,023 about 2. With all 1,023 in hand, build bottom up.

levelnumberseach climbscomparisons
0100
1212
2428
38324
4 to 72404 to 71,504
825682,048
951294,608
in all1,0238,194
One at a time, smallest first, level by level.
What count of comparisons can no row of nn numbers go past?
  1. the move: price a round

    the line:
    a round at place p comparesits children, 2p and 2p+1,then stops or drops a level\begin{gathered} \text{a round at place } p \text{ compares} \\ \text{its children, } 2p \text{ and } 2p + 1 \text{,} \\ \text{then stops or drops a level} \end{gathered}
    1. step 12 of 20

      46855467231138

      Each part below place 1 is a heap already. Sift its number, 46, down: while a child is bigger, swap it with its bigger child.

      comparisons: 0

    2. step 15 of 20

      85465467231138

      Swap 46 down with its bigger child, 85.

      comparisons: 2

    3. step 18 of 20

      85675446231138

      Swap 46 down with its bigger child, 67.

      comparisons: 4

    4. step 19 of 20

      85675446231138

      46 has no children, so it stops here.

      comparisons: 4

    The sift at place 1 of section 1's row, its comparisons counted from this sift's start. 46 meets two children and swaps below 85, then below 67, and stops at place 4, which has none. 4 comparisons.

    why it is allowed: Each child is compared with the bigger so far, the number itself first. So a round makes at most 2 comparisons. The drawing follows 46 in section 1's row.

  2. the move: count the rounds

    the line:
    a sift from place pmakes at most hp rounds\begin{gathered} \text{a sift from place } p \\ \text{makes at most } h_p \text{ rounds} \end{gathered}
    3211100000312213141506070809010
    The number in each place is its height in a row of 10. Place 3's part is places 3, 6 and 7, one level deep, so its height is 1. In all 8.

    why it is allowed: hph_p, the height of place pp, is the steps from pp down to the deepest place in its own part. A round needs a child below, so a sift makes at most that many rounds.

  3. the move: add up every sift

    the line:
    U=2(h1+h2+⋯+hn)U = 2(h_1 + h_2 + \dots + h_n)
    numberscosts the mostsmallest firstUU
    4446
    5666
    6768
    7888
    8111014
    Every order of 1 to n tried, in comparisons. No order passes UU, and the cheapest is always n−1n - 1.

    why it is allowed: UU is a count no row goes past, by lines 1 and 2, as each sift makes at most 2 comparisons in each of its rounds. For 10, 2 × 8 is 16. On a full heap, like 7 or 15, smallest first makes exactly UU. Every parent there has two children, and each sifting number is smaller than all below it. The sifts before it only move later places. So it sinks to the bottom: hph_p rounds of 2. At 7, 2 + 2 + 4 = 8.

  4. the move: a step down at least doubles

    the line:
    so p is at least k high exactlywhen place 2kp is in the row\begin{gathered} \text{so } p \text{ is at least } k \text{ high exactly} \\ \text{when place } 2^k p \text{ is in the row} \end{gathered}
    steps down from place 3places reachedin a row of 10
    16 and 7yes
    212 to 15no
    One step down from place 3 reaches places 6 and 7, two steps places 12 to 15, past the end. So place 3 is 1 high.

    why it is allowed: Place pp's children are 2p2p and 2p+12p + 1, as the box at line 3 of the question on a heap already shows. So the places kk steps under pp start at place 2kp2^k p.

  5. the move: count the places

    the line:
    so places 1 to ⌊n/2k⌋are at least k high\begin{gathered} \text{so places 1 to } \lfloor n / 2^k \rfloor \\ \text{are at least } k \text{ high} \end{gathered}

    why it is allowed: Place 2kp2^k p is in the row when pp is at most nn over 2k2^k. A place is a whole number, so at most that rounded down. For 10, at height 2, 10 over 4 is 2.5, so places 1 and 2.

  6. the move: add the heights level by level

    the line:
    U=2(⌊n/2⌋+⌊n/4⌋+⋯+⌊n/2K⌋)\begin{aligned} U = 2\bigl(&\lfloor n / 2 \rfloor + \lfloor n / 4 \rfloor \\ &+ \dots + \lfloor n / 2^K \rfloor\bigr) \end{aligned}
    placeat least 1 highat least 2 highat least 3 highheight
    11113
    2112
    311
    411
    511
    in all5218
    A row of 10, places 6 to 10 left out, being 0 high. The columns add to 5, 2 and 1, and the heights to 8.

    why it is allowed: Count each place once at every height up to its own, by line 5. KK is the top's height, and no place is higher. For 10, 5 + 2 + 1 is 8.

  7. the move: drop the rounding

    the line:
    U≤2(n2+n4+⋯+n2K)U \le 2\Bigl(\frac{n}{2} + \frac{n}{4} + \dots + \frac{n}{2^K}\Bigr)

    why it is allowed: Rounding down never makes a number bigger; 2.5 becomes 2. For 10, 5 + 2.5 + 1.25 is 8.75.

  8. the move: take n out

    the line:
    U≤2n(12+14+⋯+12K)U \le 2n\Bigl(\frac{1}{2} + \frac{1}{4} + \dots + \frac{1}{2^K}\Bigr)

    why it is allowed: Each term is nn times a halving term.

  9. the move: use the halving sum

    the line:
    U≤2n(1−12K)U \le 2n\Bigl(1 - \frac{1}{2^K}\Bigr)
    where does this come from?
    question 2 of the page doubling and halving sumsAdd 12+14+⋯+12n\frac{1}{2} + \frac{1}{4} + \dots + \frac{1}{2^n}. How close does it come to 1?
    1. the move: name the sum

      the line:
      H=12+14+⋯+12nH = \frac{1}{2} + \frac{1}{4} + \dots + \frac{1}{2^n}
      Line 1 for 3, 7 over 8, short of 1 by 1 over 8.

      why it is allowed: Let HH be the halving sum from one half down to 1 over 2n2^n. The bars add it for 3.

    2. the move: double both sides

      the line:
      2H=1+12+⋯+12n−12H = 1 + \frac{1}{2} + \dots + \frac{1}{2^{n - 1}}

      why it is allowed: As in question 1 of the page doubling and halving sums: double, take away, and all but the ends cancel. Doubled, one half becomes 1, and 1 over 2n2^n becomes 1 over 2n−12^{n - 1}, as 2n2^n is twice 2n−12^{n - 1}. For 3, 2 × 1 over 8 is 2 over 8, which is 1 over 4.

    3. the move: take line 1 away

      the line:
      2H−H=(1+⋯+12n−1)−(12+⋯+12n)\begin{aligned} 2H - H = {} & \left(1 + \dots + \frac{1}{2^{n - 1}}\right) \\ & - \left(\frac{1}{2} + \dots + \frac{1}{2^n}\right) \end{aligned}

      why it is allowed: Take HH from both sides of line 2. On the right, write HH out as line 1 does.

    4. the move: tidy the left side

      the line:
      H=(1+⋯+12n−1)−(12+⋯+12n)\begin{aligned} H = {} & \left(1 + \dots + \frac{1}{2^{n - 1}}\right) \\ & - \left(\frac{1}{2} + \dots + \frac{1}{2^n}\right) \end{aligned}

      why it is allowed: Two lots of HH less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      H=1−12nH = 1 - \frac{1}{2^n}

      why it is allowed: Every term from one half to 1 over 2n−12^{n - 1} is in both brackets and cancels, leaving 1 less 1 over 2n2^n. For 3, 1 less 1 over 8 is 7 over 8.

    6. the move: a ceiling

      the line:
      H≤1H \le 1

      why it is allowed: Line 5 takes 1 over 2n2^n, above zero, from 1, so HH is under 1, and so at most 1.

    7. the move: read it

      the line:
      ever closer to 1,never reaching it\begin{gathered} \text{ever closer to 1,} \\ \text{never reaching it} \end{gathered}

      why it is allowed: By line 5 the sum falls short of 1 by its last term, which halves each time but never reaches zero. Line 6 makes 1 its ceiling.

    the whole page: doubling and halving sums

    why it is allowed: The box under this line adds the halving sum, its nn being KK here. It falls short of 1 by its last term. For 10, KK is 3, and 1 less 1 over 8 is 7 over 8.

  10. the move: multiply out

    the line:
    U≤2n−2n2KU \le 2n - \frac{2n}{2^K}

    why it is allowed: Twice nn times each part. For 10, 20 less 2.5 is 17.5.

  11. the move: the top's path ends in the row

    the line:
    U≤2n−2U \le 2n - 2
    13223415678091012345678910
    The top's path down by first children, places 1, 2, 4 and 8, ends inside a row of 10. The small number beside each place counts its steps up from place 8, so the top is 3 high. 2 × 10 over 8 is 2.5, so UU is at most 17.5, under 18.

    why it is allowed: The top is KK high, so by line 4 place 2K2^K is in the row. So 2K2^K is at most nn, and 2n2K\frac{2n}{2^K} is at least 2. Taking away at least 2 leaves at most 2n−22n - 2.

  12. the move: read it as a bound

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

    why it is allowed: By line 11, with the fixed multiple 2.

  13. the move: every row grows in step with n

    the line:
    every row costs from n−1to 2n−2, so Θ(n)\begin{gathered} \text{every row costs from } n - 1 \\ \text{to } 2n - 2 \text{, so } \Theta(n) \end{gathered}

    why it is allowed: No row goes past UU, so none past 2n−22n - 2, by line 11, and none costs less than n−1n - 1, by the question on a heap already. Both grow in step with nn. Each round makes at most 2 comparisons and at most 1 swap, so the swaps are at most half of UU, at most n−1n - 1 by line 11. For 10, at most 8 swaps, fewer than the 10 numbers.

try it with a small nn: at n=10n = 10, line 3 gives U=16U = 16, which you can work out by hand

this tests how it compares, on the learn page

what a full answer includes
  • 8,194, adding the levels.
  • At most 2,026, which smallest first makes exactly.
  • About 8 a number against about 2, so build bottom up.

question 6about 8 minutes

Building a heap from 88, 26, 71, 45, 19, 64, 53 makes 6 comparisons, the fewest any 7 numbers can.

  1. Why can no row of 7 numbers cost fewer than 6?
  2. This row is no heap, as 26 sits over 45. Why does it still cost only 6?
  3. A build makes n−1n - 1 comparisons and no swap. Must the row have been a heap already?
a hint

A sift makes a second round only when its number lands on a place with children.

a worked answer

Every sift starts by meeting each child of its place once, and places 2 to 7 are each one parent's child: 6. Here 26 swaps with 45 into place 4, which has no children, so its sift ends with no more comparisons. 71 and 88 stop after their first round. Yes: with no swap, every sift stopped after its first round, so each parent was at least as big as its children.

88267145196453881262713454195646537
26 at place 2 meets its children, 45 and 19, and swaps with 45 down to place 4. Place 4 is a leaf, with no children, so the sift ends with no second round.
Why does a row that is a heap already cost exactly n−1n - 1 comparisons, the fewest any row can?
  1. the move: a sift's first round

    the line:
    a sift first compareseach child of its place\begin{gathered} \text{a sift first compares} \\ \text{each child of its place} \end{gathered}
    92748133601847921742813334605186477
    A heap already, 92, 74, 81, 33, 60, 18, 47. Each marked parent is at least as big as its children.
    sift at placeits childrencomparisons
    318 and 472
    233 and 602
    174 and 812
    in all6
    Each parent meets its two children, then stops. At place 1, 74 against 92 leaves 92 the bigger so far, and so does 81 against 92; 2 comparisons. 6 in all, no swap.

    why it is allowed: Each child is compared once with the bigger so far. In a heap already no child is bigger, so every sift stops after that round, and nothing moves.

  2. the move: add up the first rounds

    the line:
    B=how many children places1 to ⌊n/2⌋ haveB = \begin{gathered} \text{how many children places} \\ 1 \text{ to } \lfloor n / 2 \rfloor \text{ have} \end{gathered}

    why it is allowed: BB is the comparisons on a heap already. Place pp has a child when place 2p2p is in the row, so when pp is at most half of nn. pp is whole, so the parents are places 1 to ⌊n/2⌋\lfloor n / 2 \rfloor. At 7, 2 × 3 = 6 is in the row and 2 × 4 = 8 is not, so places 1 to 3, as section 1's picture marks. A parent has two children when 2p+12p + 1 is in the row too, else one.

  3. the move: every place but the top is a child

    the line:
    B=n−1B = n - 1
    where does this come from?
    question 2 of the page trees and their wordsFrom place ii of a tree kept in a row, which places lie one step down, and which one step up?
    1. the move: find where a level starts

      the line:
      level d startsat place 2d\begin{gathered} \text{level } d \text{ starts} \\ \text{at place } 2^d \end{gathered}
      depth012312345678910123456789100123
      The tree of 10 places. Its levels start at places 1, 2, 4 and 8, each block in the row starting where the one before ends.

      why it is allowed: Say place ii is on level dd. No level starts until the one above it is used up, so the levels above level dd are full. For level 3, levels 0 to 2 hold 1, 2 and 4, which is 7 places, so level 3 starts at place 8. In general the levels above form a full tree whose height is one less than dd. By question 1 of the page trees and their words it holds one place fewer than 2d2^d, so level dd starts at place 2d2^d. Level 0 is the root alone, at place 1, and 2 to the power 0 is 1.

    2. the move: count what comes before the first child

      the line:
      C=2d+1+2(i−2d)C = 2^{d + 1} + 2(i - 2^d)
      1234567891012345678910
      Places 2 and 3 take their children in turn, 4 and 5, then 6 and 7. So the first child of place 3 comes after the two of place 2.

      why it is allowed: Let CC be the place of the first child of ii. On level dd, i−2di - 2^d places come before ii. The row fills level d+1d + 1 from its left end, each place's children in turn, so each place before ii has its two children there before ii's first. That level starts at place 2d+12^{d + 1}. For place 3, one place comes before it on level 1, so its first child is 4 + 2, place 6.

    3. the move: multiply out

      the line:
      C=2d+1+2i−2⋅2dC = 2^{d + 1} + 2i - 2 \cdot 2^d

      why it is allowed: Twice i−2di - 2^d is twice ii less twice 2d2^d.

    4. the move: tidy the power

      the line:
      C=2d+1+2i−2d+1C = 2^{d + 1} + 2i - 2^{d + 1}

      why it is allowed: Twice 2d2^d is 2d+12^{d + 1}.

    5. the move: cancel

      the line:
      C=2iC = 2i

      why it is allowed: The first and last terms cancel. For place 3, twice 3 is 6, as line 2 found.

    6. the move: the second child is next

      the line:
      its second childis place 2i+1\begin{gathered} \text{its second child} \\ \text{is place } 2i + 1 \end{gathered}

      why it is allowed: The two children stand side by side. For place 3, places 6 and 7. A place whose first child would fall past the end of the row is a leaf.

    7. the move: halve a first child

      the line:
      ⌊2i/2⌋=i\lfloor 2i / 2 \rfloor = i

      why it is allowed: The floor brackets round down, as on the page on halving. Half of 2i2i is ii exactly, so rounding changes nothing.

    8. the move: halve a second child

      the line:
      ⌊(2i+1)/2⌋=i\lfloor (2i + 1) / 2 \rfloor = i

      why it is allowed: Half of 2i+12i + 1 is ii and a half, which rounds down to ii.

    9. the move: read it

      the line:
      past the root, the parentof place i is place ⌊i/2⌋\begin{gathered} \text{past the root, the parent} \\ \text{of place } i \text{ is place } \lfloor i / 2 \rfloor \end{gathered}

      why it is allowed: Every place but the root is the first or the second child of some place, twice it or one more, by lines 5 and 6. Halving it and rounding down gives that place back, by lines 7 and 8. For places 6 and 7, place 3.

    the whole page: trees and their words

    why it is allowed: The box under this line shows each place but the top has exactly one place above it. So places 2 to nn are each counted once. For 7, 2 + 2 + 2 is 6; for 6, place 3 has one child, so 2 + 2 + 1 is 5.

  4. the move: no row costs fewer

    the line:
    every row makesthese first rounds\begin{gathered} \text{every row makes} \\ \text{these first rounds} \end{gathered}

    why it is allowed: On any row, each sift starts by meeting its place's children, so no row costs less than n−1n - 1. A row of 7, 7, 7, 7, 7, 7, 7 costs exactly 6, as no child is bigger.

  5. the move: which rows cost that

    the line:
    a row with no swapwas a heap already\begin{gathered} \text{a row with no swap} \\ \text{was a heap already} \end{gathered}

    why it is allowed: With no swap, every sift stopped at once, so each parent was at least as big as its children. A swap into a place with no children adds no comparison, so some rows with swaps cost n−1n - 1 too. In the row 88, 26, 71, 45, 19, 64, 53, the 26 swaps into place 4, which has no children, and the row costs 6.

  6. the move: a ceiling

    the line:
    B≤nB \le n

    why it is allowed: By line 3, taking 1 away.

  7. the move: read it as a bound

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

    why it is allowed: By line 6, with the fixed multiple 1.

  8. the move: a floor

    the line:
    B≥n2 for n≥2B \ge \frac{n}{2} \text{ for } n \ge 2

    why it is allowed: Half of nn is at most n−1n - 1 from 2 on. At 7, 3.5 is under 6.

  9. the move: read it as a bound

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

    why it is allowed: By line 8, with the fixed multiple a half.

  10. the move: ceiling and floor together

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

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

try it with a small nn: at n=7n = 7, line 2 gives B=6B = 6, which you can work out by hand

this tests best, worst and average, on the learn page

what a full answer includes
  • Each of places 2 to 7 is met once by its parent.
  • 26 lands on a place with no children, so there is no second round.
  • Yes: no swap means no child was bigger than its parent.