An empty chain kept in order puts in 30, 12 and 45, takes 12 out, then puts in 20. What does the chain hold then, from the start?
the answer, and why
the answer is B
After 12 leaves, the chain holds 30, 45. 30 is bigger than 20, so the first look stops the walk and 20 goes in at the start.
step 10 of 12
30 is bigger than 20, so 20 goes just before it.
looks so far: 1
step 11 of 12
Link 20 in at the start of the chain.
looks so far: 1
the start, holding no number
a look, numbered in turn
looked at now
just linked in, with its new pointers
Operation 5 puts 20 in, after operations 1 to 4 put in 30, 12 and 45 and took 12 out. 30 is bigger than 20, so 20 is linked in at the start.
if you chose another
AThat keeps 12, but the fourth operation takes 12 out.
CThat puts 20 at the end, but a put stops at the first bigger number, 30.
DThat loses 30. Taking 12 out links the start to 30, which stays.
2question 2
Starting from an empty chain kept in order, you put in 24, 9, 31 and 17, in that order. How many looks do the four puts make in all?
the answer, and why
the answer is A
24 finds the chain empty, 0 looks. 9 stops at 24, 1. 31 passes 9 and 24, 2. 17 passes 9 and stops at 24, 2. That is 5.
step 8 of 11
9 is not bigger than 17, so walk on past it.
looks so far: 1
step 9 of 11
24 is bigger than 17, so 17 goes just before it.
looks so far: 2
step 10 of 11
Link 17 in just before 24, and point it at 24.
looks so far: 2
the start, holding no number
a look, numbered in turn
looked at now
looked at, and walked past
just linked in, with its new pointers
Operation 4 puts 17 in, after operations 1 to 3 put in 24, 9 and 31. It passes 9, stops at 24 and links 17 in between, 2 looks.
if you chose another
BThat counts every place each time, as smallest first would, 0 + 1 + 2 + 3.
CThat misses a look. 31 passes both 9 and 24 to reach the end.
DThat counts 1 look a put after the first, as biggest first would.
3question 3
Which order of putting 14, 26, 38 and 51 into an empty chain costs the fewest looks?
the answer, and why
the answer is A
Biggest first, each put after the first stops at its first look. 0 + 1 + 1 + 1 is 3.
step 7 of 9
26 is bigger than 14, so 14 goes just before it.
looks so far: 1
step 8 of 9
Link 14 in at the start of the chain.
looks so far: 1
the start, holding no number
a look, numbered in turn
looked at now
just linked in, with its new pointers
Biggest first, operation 4 puts 14 in, after 51, 38 and 26 went in. 26 is bigger than 14, so one look, and 14 goes in at the start.
if you chose another
BThat costs 5. 38 passes 14 and 26 before it stops at 51.
CSmallest first is the dearest order, 0 + 1 + 2 + 3, which is 6.
DThat costs 5. 51 passes both numbers before it to reach the end.
4question 4
Sebastian Bourges' books put shuffled numbers in order one at a time, in a chain and in a row, each put scanning from the front (volume two, chapter 22). Which is true?
the answer, and why
the answer is B
Section 4's row question counts the row at about twice the chain from about 100 numbers on. The clock still favoured the row: each read of a chain waits for the pointer before it.
read in this wait, numbered in turn
not read yet
read in an earlier wait
other memory
a pointer read, giving the next address
read at once, every address known ahead
The same 4 numbers, read from a row all at once, and from a chain one wait a read. The next address is known only when the read before it ends.
AA chain has no middle to jump to. Its places are reached only from the start.
C says how a count grows, not how long each look takes.
DA put into the row moves every bigger number one slot along.
pen and paper
5question 5about 6 minutes
Put 4, 11, 19, 26, 33 and 47 into an empty chain kept in order, in that order.
How many looks does each put make?
How many in all?
Check your total against , the formula of section 4.
a hint
Each new number is bigger than all before it, so it passes every place.
a worked answer
The puts make 0, 1, 2, 3, 4 and 5 looks, as each passes every number already in. That is 15 in all. With 6 numbers, 5 times 6, halved, is 15.
put
chain before
looks
4
empty
0
11
4
1
19
4, 11
2
26
4, 11, 19
3
33
4, 11, 19, 26
4
47
4, 11, 19, 26, 33
5
in all
15
Each put passes the whole chain and links its number at the end.
Why do numbers put in smallest first cost looks, the most any order can?13 of 13 shown
1the move: price one put
the line:
step 8 of 12
3 is not bigger than 40, so walk on past it.
looks so far: 1
step 9 of 12
16 is not bigger than 40, so walk on past it.
looks so far: 2
step 10 of 12
29 is not bigger than 40, so walk on past it.
looks so far: 3
step 11 of 12
Link 40 in at the end of the chain.
looks so far: 3
the start, holding no number
a look, numbered in turn
looked at now
looked at, and walked past
just linked in, with its new pointers
Operation 4 puts 40 in, after 3, 16 and 29 went in, smallest first. 40 is bigger than all three, so the walk passes them and links 40 at the end. 3 looks.
why it is allowed: Here is how many numbers the chain holds. The walk looks at each place once, from the start, and stops at the first bigger number or at the end.
2the move: smallest first, every place
the line:
why it is allowed: Each new number is bigger than all before it, so no place stops the walk: the th put passes all numbers. Four 24s cost the same, 0 + 1 + 2 + 3, as no 24 stops the walk. For 4, the fourth put makes 3.
guess it, then
3the move: no order costs more
the line:
why it is allowed: The th put meets the numbers already in, so by line 1 it makes at most looks. So no order costs more than line 2's.
guess it, then
4the move: add the puts
the line:
why it is allowed: is the looks of numbers put in smallest first. By line 2, puts 1 to make 0, 1, and on up to . For 4, 0 + 1 + 2 + 3 is 6.
guess it, then
5the move: leave out the 0
the line:
why it is allowed: Line 4 without its 0, as adding 0 changes nothing.
guess it, then
6the move: use the page on adding up
the line:
where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make?
1the move: draw the sum as a staircase
the line:
why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
2the move: take two staircases
the line:
why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
3the move: fit them into a rectangle
the line:
Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.
why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
4the move: halve both sides
the line:
why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. Here the last number is . For 4, 1 + 2 + 3 is 3 times 4, halved, which is 6.
guess it, then
7the move: tidy
the line:
why it is allowed: In line 6, is . For 5, 4 times 5, halved, is 10.
guess it, then
8the move: a ceiling
the line:
why it is allowed: In line 7, is less than , so is at most times . For 5, 10 is at most 12.5.
guess it, then
9the move: read it as a bound
the line:
why it is allowed: By line 8, with the fixed multiple one half.
guess it, then
10the move: a fact about n
the line:
why it is allowed: Taking 1 from takes at most half of it once is 2 or more. For 5, 4 is at least 2.5.
guess it, then
11the move: a floor
the line:
why it is allowed: In line 7, put line 10's half of for , which is no bigger. Half of times , halved, is a quarter of . For 5, 10 is at least 6.25.
guess it, then
12the move: read it as a bound
the line:
why it is allowed: By line 11, from 2 numbers on.
guess it, then
13the move: ceiling and floor together
the line:
why it is allowed: Lines 9 and 12 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
Put 7, 15 and 22 into an empty chain kept in order, in an order picked at random, each of the 6 orders as likely.
Count the looks of each order.
Average them.
Check your average against at 3.
a hint
The second put always makes 1 look. The third makes 1 look when it is the smallest, and 2 otherwise.
a worked answer
Four orders cost 3 looks and two cost 2: 16 in all, and 16 over 6 is about 2.67. In the formula, 2 times 3 over 4 is 1.5, and is 1 + 1 over 2 + 1 over 3, 11 over 6. In sixths, 1.5, 3 and are 9, 18 and 11, and 9 + 18 less 11 is 16: 16 over 6 too.
order
looks by each put
looks
7, 15, 22
0, 1, 2
3
7, 22, 15
0, 1, 2
3
15, 7, 22
0, 1, 2
3
15, 22, 7
0, 1, 1
2
22, 7, 15
0, 1, 2
3
22, 15, 7
0, 1, 1
2
in all
16
on average
16 over 6, about 2.67
The 6 orders of 7, 15 and 22. The two that put 7 last cost 2.
Why do shuffled numbers cost looks on average?13 of 13 shown
1the move: price one put by what it passes
the line:
step 8 of 11
8 is not bigger than 14, so walk on past it.
looks so far: 1
step 9 of 11
20 is bigger than 14, so 14 goes just before it.
looks so far: 2
step 10 of 11
Link 14 in just before 20, and point it at 20.
looks so far: 2
the start, holding no number
a look, numbered in turn
looked at now
looked at, and walked past
just linked in, with its new pointers
Operation 4 puts 14 in, after 20, 8 and 33 went in. It passes 8, the one smaller number, then looks at 20 and stops. 1 + 1 is 2 looks.
why it is allowed: The numbers are all different. The walk passes every smaller number, then stops at the first bigger one. A new best, bigger than all before it, finds none to stop at.
2the move: add over the puts
the line:
put
smaller numbers passed
plus 1
less 1 for a new best
looks
2
0
1
1
0
3
1
1
1
1
1
0
1
0
1
in all
1
3
2
2
Putting in 2, 3, then 1. Each put's looks are the smaller numbers it passes, plus 1, less 1 for a new best.
why it is allowed: Line 1, added over the puts. Each smaller number a put passes makes one pair in order, the smaller put in first. On 2, 3, 1: 1 pair in order, plus 3, less 2 new bests, is 2.
guess it, then
3the move: average piece by piece
the line:
order put in
pairs in order
new bests
looks
1, 2, 3
3
3
3
1, 3, 2
2
2
3
2, 1, 3
2
2
3
2, 3, 1
1
2
2
3, 1, 2
1
1
3
3, 2, 1
0
1
2
in all
9
11
16
Each order of 1, 2 and 3. Its looks are its pairs in order, plus 3, less its new bests. In all, 9 + 18 less 11 is 16, and the 18 is 3 for each of the 6 orders.
why it is allowed: is the looks averaged over every order of different numbers. By line 2, each order's looks are its pairs in order, plus , less its new bests. Averages add piece by piece: add each piece over every order, then divide by how many orders there are. For 3, 9 over 6 + 3 less 11 over 6, which is 16 over 6.
guess it, then
4the move: use the page on expected cost
the line:
where does this come from?
question 2 of the page expected costShuffle different numbers at random. How many pairs stand out of order, on average?
1the move: out of order half the time
the line:
Trading places 1 and 3 pairs each shuffle holding them in order with one holding them out of order, 3 and 3.
shuffle
places 1 and 2
places 1 and 3
places 2 and 3
pairs out of order
1, 2, 3
0
1, 3, 2
yes
1
2, 1, 3
yes
1
2, 3, 1
yes
yes
2
3, 1, 2
yes
yes
2
3, 2, 1
yes
yes
yes
3
in all
3
3
3
9
Each shuffle of 1, 2 and 3, a yes where a pair of places is out of order. 9 over 6 is 1.5.
why it is allowed: A pair is two places. Trading their numbers pairs each shuffle with another, and trading back undoes it. Each two has the pair out of order in exactly one, and both are as likely: chance one half.
2the move: expected counts add
the line:
why it is allowed: The table at line 1 adds two ways: down its last column, 9, and along its bottom row, 3 + 3 + 3. Over 6 shuffles, 9 over 6 is the average count, 1.5, and 3 over 6 three times is the pairs' chances added, 1.5 too. Both add the same yeses, so they agree for any , though the pairs depend on each other. With unequal chances, weight each yes by its order's chance. 2, 1, 3 at 3 over 4 and 1, 2, 3 at 1 over 4 give 3 over 4 both ways.
3the move: one half for each pair
the line:
why it is allowed: By line 2 the expected count adds line 1's one half once per pair.
4the move: count the pairs
the line:
why it is allowed: Let be the expected count, by line 3 half the number of pairs. The first place pairs with the after it, the next with one fewer, down to 1. For 3 places, 2 + 1 is 3 pairs, so 1.5.
5the move: add them the other way round
the line:
why it is allowed: The same numbers in the other order.
6the move: use the page on adding up
the line:
where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make?
1the move: draw the sum as a staircase
the line:
why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
2the move: take two staircases
the line:
why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
3the move: fit them into a rectangle
the line:
Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.
why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
4the move: halve both sides
the line:
why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The page on adding up 1 to adds the numbers from 1 to any whole number. They add up to that number times one more than it, halved: 1 + 2 + 3 is 3 times 4, halved, which is 6. In its working, its sum runs from 1 up to its own last number, which it calls . Here that is .
7the move: tidy the bracket
the line:
why it is allowed: is .
8the move: multiply the halves
the line:
why it is allowed: Half of a half is a quarter. For 3, 2 times 3 over 4 is 1.5.
9the move: read it
the line:
why it is allowed: By lines 4 to 7 there are pairs, and line 8 is half that. For 3, 1.5, as line 1 counted.
why it is allowed: There are 1 + 2 + ... + pairs, each in order in half the orders. Line 3's table holds 9 in order over the 6 orders, and the box's table 9 out of order. So both average the same, , which the box works out. For 3, 9 over 6 is 1.5.
guess it, then
5the move: use the page on expected cost again
the line:
where does this come from?
question 3 of the page expected costWalk along different numbers shuffled at random. How many new bests turn up, on average?
1the move: a new best at place k
the line:
Trading place 3 with place 1 or 2 moves the 3 there, and trading back undoes it. 2 shuffles hold the 3 at each place.
shuffle
place 1
place 2
place 3
new bests
1, 2, 3
yes
yes
yes
3
1, 3, 2
yes
yes
2
2, 1, 3
yes
yes
2
2, 3, 1
yes
yes
2
3, 1, 2
yes
1
3, 2, 1
yes
1
in all
6
3
2
11
Each shuffle of 1, 2 and 3, a yes where a place holds a new best. 11 over 6 is 1 + 1 over 2 + 1 over 3.
why it is allowed: The number at place is a new best when it is the biggest of the first . Trade place with another of the first : a biggest at one moves to the other, and trading back undoes it. So it stands equally often at each of the first places: at place in 1 of every shuffles. For 3, the 3 stands at each place in 2 of 6.
2the move: add the chances
the line:
why it is allowed: Let be the expected new bests. Each place is a piece, a new best or not. By line 2 of question 2 of the page expected cost, adds their chances: 1 over for place , by line 1.
3the move: read it
the line:
why it is allowed: By line 2, the harmonic sum. For 3, 11 over 6, about 1.83, as line 1 counted.
why it is allowed: The th put is a new best with chance 1 over , and the chances add: the box under this line shows both. For 3, 1 + 1 over 2 + 1 over 3 is 11 over 6, line 3's.
guess it, then
6the move: name the harmonic sum
the line:
why it is allowed: Line 5's bracket is the harmonic sum, . For 3, about 1.83, so is about 2.67, line 3's 16 over 6.
guess it, then
7the move: a ceiling from the dearest order
the line:
why it is allowed: No order costs more than smallest first, by the smallest-first question's line 3 and its line 7. So neither does the average. For 3, 2.67 is at most 3.
guess it, then
8the move: a simpler ceiling
the line:
why it is allowed: As in the smallest-first question's line 8, is at most times . For 3, 2.67 is at most 4.5.
guess it, then
9the move: read it as a bound
the line:
why it is allowed: By line 8.
guess it, then
10the move: a floor
the line:
why it is allowed: adds numbers, each at most 1, so in line 6 less is at least 0. For 3, 2.67 is at least 1.5.
guess it, then
11the move: a simpler floor
the line:
why it is allowed: In line 10, put half of for , which is no bigger by the smallest-first question's line 10. For 4, line 10's 3 is at least 2.
guess it, then
12the move: read it as a bound
the line:
why it is allowed: By line 11, from 2 numbers on.
guess it, then
13the move: ceiling and floor together
the line:
why it is allowed: Lines 9 and 12 bound by the same growth, . At 1,000 numbers, is about 7.5, beside about 250,000 pairs in order on average, of 499,500 pairs.
guess it, then
try it on small inputs: over every order of 3 numbers put in, the average is about 2.67