A studio takes five bookings. P runs from 3 to 6, Q from 2 to 4, R from 6 to 9, S from 0 to 7 and T from 1 to 3. Which does earliest finish first choose?
the answer, and why
the answer is A
In order of ends, T, Q, P, S, R. T is chosen, free from 3. Q starts at 2, so it is ruled out. P starts at 3, so it is chosen, free from 6. S is ruled out. R starts at 6, so it is chosen.
BThat is earliest start first. S starts at 0 and runs till 7, so it clashes with all the rest.
CThat is shortest first. Q and T are 2 hours each, and Q, typed first, is taken first. Q then rules out T, P and S, and R fits after it.
DT runs till 3 and Q starts at 2, so they clash.
2question 2
These four activities are typed in this order: A from 0 to 1, B from 2 to 3, C from 1 to 2 and D from 3 to 4. The drawing lines them up by end. How many comparisons does the sort make?
the answer, and why
the answer is A
Round 1: A with B, then C with D, 1 comparison each. Round 2: ends 1, 3 with 2, 4 compares 1 with 2, 3 with 2 and 3 with 4. That is 3, so 5 in all.
if you chose another
B4 is the drawing's order, ends 1, 2, 3, 4; the sort meets them as typed, 1, 3, 2, 4.
CThat counts only the checks after the sort, one for each activity after the first.
DThat counts round 2 as 4, but the last activity follows with no comparison.
3question 3
Which list makes the sort compare the fewest times?
the answer, and why
the answer is A
Latest end first, every right run is used up first. 2 + 2 + 1, which is 5.
if you chose another
BIn end order, every left run is used up first, and a left run can be the longer. 2 + 2 + 4, which is 8.
CThat is the dearest order, 2 + 3 + 4, which is 9.
DRound 1 puts each swapped pair in end order, 1 comparison each. That leaves end order's runs, so 2 + 2 + 4, which is 8.
4question 4
A booking system takes 1,000 requests for one hall. Which is true, whatever order they come in?
the answer, and why
the answer is D
Every request after the first is checked once against the hour the hall is free from.
AA request that clashes is still checked, then ruled out.
BThe order moves the comparisons, from 4,932 to 8,985 at 1,000.
CThe sort makes at least 4,932 comparisons, against 999 checks.
pen and paper
5question 5about 6 minutes
A lab bench takes five bookings. A runs from 2 to 5, B from 0 to 3, C from 4 to 7, D from 3 to 4 and E from 6 to 8.
Line them up by the hour each ends.
Use earliest finish first: which are chosen and which ruled out?
How many checks did you make?
a hint
Keep the hour the bench is free from. A booking fits if it starts at that hour or later.
a worked answer
By end: B at 3, D at 4, A at 5, C at 7, E at 8. B is chosen, free from 3. D starts at 3: chosen, free from 4. A starts at 2: ruled out. C starts at 4: chosen, free from 7. E starts at 6: ruled out. B, D and C are chosen, with 4 checks, one for each booking after B.
The five bookings, A to E. Earliest finish first takes B, D and C.
A hall's booking system sorts 1,024 requests with the watch page's merge sort, then uses earliest finish first.
How many comparisons latest end first?
How many in the dearest order?
How many checks, in either order?
a hint
1,024 is ten 2s multiplied together, so no run ever waits. Questions 1 to 3 of the learn page's section 4 count each.
a worked answer
No run waits, and each of the 10 rounds' right runs hold half the requests, 512. Latest end first, each right run is used up first: 10 times 512, which is 5,120. In the dearest order, 1,024 times 10, less 0 waiting, less 1,023 for the merges: 9,217. The checks are 1,023 either way.
Question 1: how many comparisons, , does the sort make on activities typed latest end first, from 2 up?11 of 11 shown
1the move: how the sort goes
the line:
5 numbers stand for 5 activities' ends, latest first. Round 2's merge, 4, 5 with 2, 3, compares 2 with 4, then 3 with 4. Then the right run is used up, and 4 and 5 follow with no comparison.
round
run length
merges
comparisons
1
1
2
2
2
2
1
2
3
4
1
1
all
4
5
Five activities, latest end first. In rounds 1 and 2 the last run waits.
why it is allowed: The watch page's sort is merge sort, from the bottom up. Each activity starts as a run of 1. Each round merges neighbouring runs, comparing their front activities and taking the one that ends first. A last run with no partner waits. Every run is 1, 2, 4 and on long, except perhaps a round's last, so a right run is never the longer. Which runs meet depends only on , never on the order.
2the move: a merge's least
the line:
why it is allowed: A merge stops when one run is used up, and each activity of that run needed one comparison to leave it. By line 1, the right run is never the longer. So whichever run is used up holds at least the right run's length. For 4, 5 with 2, 3, at least 2 comparisons.
guess it, then
3the move: latest end first, the right run is used up first
the line:
run length
right runs
comparisons
1
5 of 1
5
2
2 of 2
4
4
1 of 4
4
8
1 of 2
2
all
15
Ten activities, latest end first. At run length 2 the last run waits, and at 4 the last 2 activities wait. At 8 the right run holds only those 2.
why it is allowed: Latest end first, each right run's activities end before the left run's beside it. So the right run is used up first, one comparison each, the least line 2 allows. adds up the right runs in the round of runs of 1, of runs of 2, and so on. For 5, 2 + 2 + 1.
guess it, then
4the move: a ceiling
the line:
Top, numbers, each double the last; bottom, their logs, each 1 more. 5 lies between 4 and 8, so its log, about 2.3, rounds up to 3. 10 lies between 8 and 16, so its log, about 3.3, rounds up to 4.
why it is allowed: In a pair the right run is never the longer, so a round's right runs hold at most half the activities, . Run lengths double until one run holds all , so there are rounds. Multiply the two. For 5, half of 5 times 3 rounds is 7.5, and is 5.
guess it, then
5the move: a simpler ceiling
the line:
why it is allowed: Rounding up adds less than 1, and from 2 up 1 is at most . So is at most twice , and times twice is . For 5, is 5, at most 11.6.
guess it, then
6the move: read the bound
the line:
why it is allowed: By line 5, with the fixed multiple 1.
guess it, then
7the move: a floor
the line:
run length
full pairs hold
their right runs
a quarter of 5
1
4
2
1.25
2
4
2
1.25
Five activities. In each round with a full pair, the full pairs hold 4 of the 5, at least half, and their right runs 2, at least a quarter. 2 such rounds, so at least 2.5.
why it is allowed: Take a round whose runs are long, with at most , so at least one full pair of runs fits. What is left over is shorter than a pair, so the full pairs hold at least half the activities, and their right runs at least a quarter, . Such rounds have as 1, 2, 4 and on, while is at most , so there are of them. By line 3, adds up every round's right runs, so multiply the two. For 5, rounds of 1 and 2, so at least 2.5.
guess it, then
8the move: the rounded log against the exact one
the line:
why it is allowed: Call line 7's rounded-down log . Rounding down took off less than 1, so is below . From 2 up is at least 1, so is at most . For 5, is 2, and the log of 5, about 2.3, is below 3, at most 4.
guess it, then
9the move: drop the rounding
the line:
why it is allowed: By line 8, is at least half of . Put that into line 7. For 5, is 5, at least 1.45.
guess it, then
10the move: read the bound
the line:
why it is allowed: By line 9, with the fixed multiple an eighth.
guess it, then
11the move: both together
the line:
why it is allowed: Lines 6 and 10 bound by the same growth.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand