Skip to content

when the size is not a power of two

foundations

the puzzle

A job takes k×2kk \times 2^k steps for 2k2^k items, so 28=2562^8 = 256 items take 8×256=20488 \times 256 = 2048 steps. Only powers of 2 are counted. What can be said about 200 items?

a worked example

Suppose more items never mean fewer steps. Use the two powers of 2 on either side of 200.

  1. 200 lies between 27=1282^7 = 128 and 28=2562^8 = 256.
  2. 200 items take no more steps than 256 items, so at most 2048.
  3. They take no fewer steps than 128 items. 128=27128 = 2^7, so k=7k = 7, and 128 items take 7×128=8967 \times 128 = 896 steps.
  4. So 200 items take between 896 and 2048 steps, without ever counting 200 itself.
itemspower of 2 belowpower of 2 abovesteps at leaststeps at most
2001282568962048
20163264160
30025651220484608
Each size's count lies between the counts at the powers of 2 around it.

how it works

Every whole nn from 1 up lies between two powers of 2, 2k≤n<2k+12^k \le n < 2^{k + 1}. A power of 2 is its own power below: at n=128=27n = 128 = 2^7, k=7k = 7 and the power above is 256.

0123456789101112
At 6 items, half of 6 is 3, the powers of 2 either side are 4 and 8, and twice 6 is 12.

Double both sides of 2k≤n2^k \le n: the power above, 2k+12^{k + 1}, is at most 2n2n.

Halve both sides of n<2k+1n < 2^{k + 1}: the power below, 2k2^k, is more than n2\frac{n}{2}.

At n=6n = 6, 3<4≤6<8≤123 < 4 \le 6 < 8 \le 12, as the line shows.

in symbols

Say a count T(n)T(n) never drops as nn grows. If 2k≤n<2k+12^k \le n < 2^{k + 1}, then T(2k)≤T(n)≤T(2k+1)T(2^k) \le T(n) \le T(2^{k + 1}).

For the job above, T(2k)=k×2kT(2^k) = k \times 2^k, and k=⌊log⁡2 n⌋k = \lfloor \log_2\, n \rfloor (lesson 6.4), so k≤log⁡2 nk \le \log_2\, n.

An upper bound: the power above is at most 2n2n, so T(n)≤(k+1)×2k+1≤(log⁡2 n+1)×2nT(n) \le (k + 1) \times 2^{k + 1} \le (\log_2\, n + 1) \times 2n.

A lower bound: n<2k+1n < 2^{k + 1} gives 2k>n22^k > \frac{n}{2} and k>log⁡2 n−1k > \log_2\, n - 1, so T(n)≥k×2k>n2(log⁡2 n−1)T(n) \ge k \times 2^k > \frac{n}{2}(\log_2\, n - 1).

At n=200n = 200, k=7k = 7 and log⁡2 200\log_2\, 200 is between 7 and 8. So T(200)≤8×256=2048≤9×400=3600T(200) \le 8 \times 256 = 2048 \le 9 \times 400 = 3600, and T(200)≥7×128=896>100×6=600T(200) \ge 7 \times 128 = 896 > 100 \times 6 = 600. The formulas use nn in place of the powers, so they are looser.

Both bounds grow like n log⁡2 nn \,\log_2\, n (lessons 7.3 to 7.5), so this job grows like n log⁡2 nn \,\log_2\, n at every size, not only at powers of 2.

Jeff Erickson's free notes Algorithms, appendix II, also work merge sort's count out only at powers of 2 first. They then show by another method that it holds for every size.

nn5n5n0 for odd nn, n2n^2 for even nn
150
2104
3150
42016
5250
63036
7350
84064
Rows 1, 2, 4 and 8 are powers of 2. The second count falls to 0 at every odd size, so at 5 it is below its count at 4.

mind these

  • The bounds are sure when the count never drops. For the growth to carry over too, doubling the size must at most multiply the count by a fixed number, as n2n^2 by 4 and nn by 2. A count of 2n2^n fails, as doubling the size squares it. At 5 items, 16≤32≤25616 \le 32 \le 256, so the bounds hold, but 256 is 16×1616 \times 16, and that gap keeps widening.

your turn

Work each one out. Where there is a box, type your answer and check it. Each exercise's worked answer opens under it.

exercise 1

A size n=300n = 300.

A number, like 13, 9.5 or 19/2.

A number, like 13, 9.5 or 19/2.

worked answer
  1. 28=2562^8 = 256 is below 300, and 29=5122^9 = 512 is above it.
  2. 2n=6002n = 600, and 512 is at most 600, as the rule says.

Check it another way: 28=256≤3002^8 = 256 \le 300, and 2×256=5122 \times 256 = 512 is above 300, so 300 lies between 256 and 512.

exercise 2

A job's steps never drop as nn grows, and for 2k2^k items it takes 3×2k3 \times 2^k steps.

A number, like 13, 9.5 or 19/2.

A number, like 13, 9.5 or 19/2.

worked answer
  1. 300 lies between 256 and 512.
  2. At most, 3×512=15363 \times 512 = 1536 steps. At least, 3×256=7683 \times 256 = 768 steps.

Check it another way: Both bounds are 3 times a power of 2, and 768 is half of 1536, as the powers are.

exercise 3

A job's steps never drop as nn grows, and for 2k2^k items it takes k×2kk \times 2^k steps.

A number, like 13, 9.5 or 19/2.

worked answer
  1. 50 lies between 25=322^5 = 32 and 26=642^6 = 64.
  2. At 64 items, k=6k = 6, so 6×64=3846 \times 64 = 384 steps, the most 50 items can take.

Check it another way: log⁡2 50\log_2\, 50 is between 5 and 6, as 32<50<6432 < 50 < 64. So the bound (log⁡2 n+1)×2n(\log_2\, n + 1) \times 2n at n=50n = 50 is below 100×7=700100 \times 7 = 700, and 384 is below that.

exercise 4

Three counts, each known at powers of 2.

The count that can be smaller at nn than at the power of 2 below nn:

worked answer
  1. 5n5n and n2+1n^2 + 1 never drop as nn grows, so each stays between its counts at the powers of 2 around nn.
  2. The second count is 0 at every odd nn from 3. The power of 2 under nn is even, so its count is that power squared, at least 4. So 0 falls below it.

Check it another way: At n=5n = 5 the second count is 0, though at 4 it is 16 and at 8 it is 64.