A job does 6 set-up steps. Then a loop of passes does 5 steps on each pass, and on pass also runs an inner loop of passes at 3 steps each. How does it grow?
1a worked example
Count first, name last.
Set-up is 6 steps. The loop's own 5 steps a pass make .
The inner loop runs times in all, by lesson 7.2, at 3 steps each, so .
Adding the parts, .
Check at . By hand, . The formula gives .
Multiplied out, .
As ,, so .
The part that ends up biggest is . Drop the in front, and it grows like .
part
steps
grows like
set-up
6
the loop's own steps
the inner loop
the whole job
The whole job grows like its part that ends up biggest.
2how it works
Each common growth has a name. A count that stays fixed is constant. One like is logarithmic, and one like linear.
grows like
name
a job that does it
constant
a fixed number of steps
logarithmic
a counter halved down to 1
linear
one loop over the items
n log n
a counter halved down to 1, each pass a loop over all
quadratic
a loop inside a loop
cubic
three loops each inside the last
exponential
steps that double each time one item is added
The names in the order lesson 7.3 lists them, each ending up bigger than the one above.
One like is quadratic, like cubic, and like exponential. is said "n log n".
The table pairs each name with a job that makes it.
3in symbols
The whole method is four moves. Count each part, steps in one pass times the number of passes, closing any sum with day 5's totals.
Join the parts. Add parts that run one after the other, and multiply a loop inside a loop.
Check the full count on a small . Only then name the part that ends up biggest, without its number in front.
The study pages write "grows like " as , said "theta of n squared". So the puzzle's job is .
the inner loop,
the loop's own steps,
set-up, 6
The inner loop's steps pass the loop's own from on and pull away, while the set-up stays at 6.
mind these
The count is exact. The name is for comparing methods: a quadratic count ends up bigger than a linear one.
4your turn
Work each one out. Where there is a box, type your answer and check it. Each exercise's worked answer opens under it.
1exercise 1
Give each count's growth its name.
worked answer
keeps growing, so ends up biggest, and the count is cubic.
ends up past the fixed 3. Drop the 8 in front, and it is logarithmic.
comes after in lesson 7.3's list, so the count is quadratic.
Check it another way: Each name is the growth of the part latest in lesson 7.3's list.
2exercise 2
A job does 10 set-up steps and a loop over files at 4 steps a file. Then a counter starts at and is halved, rounding down, while it is above 1. On each pass a loop reads all files, at 1 step a file.
worked answer
The first loop takes steps.
The counter runs at 64, 32, 16, 8, 4 and 2, so 6 passes, each reading all 64 files, .
With set-up, .
In general , with lesson 6.3's floor.
Rounding down takes off less than 1 pass, so less than steps in all, and the biggest part still grows like . The job is n log n.
Check it another way: At 64, , already past .
3exercise 3
A job does 7 set-up steps. Then on day of days, a loop restacks crates, at 1 step a crate.
worked answer
The crates are , which is twice .
So by lesson 7.2 they are .
With the 7 set-up steps, .
, so the part that ends up biggest is , and the job is quadratic.
Check it another way: At ,, so 49, and .
4exercise 4
A job is a loop inside a loop, both over items, so it is quadratic.
worked answer
Halving the steps puts in front, and running twice puts 2 in front. Neither changes the growth.
An inner loop of 5 passes makes steps, which is linear.
Check it another way: Only a change to how the passes grow with changes the name.
5exercise 5
Three loops sit each inside the last. The outer two run over items, and the innermost runs 10 times, at 1 step a pass.
worked answer
The steps are .
At ,.
Drop the 10 in front, and it grows like , quadratic.
Check it another way: Doubling to 14 gives , 4 times 490, as a quadratic does.