the sorting floor
why it works
what it is
A comparing sort with no random choices takes at least 5 comparisons on its worst row of 4 different numbers.
A comparing sort learns about different numbers only by asking of two, 'is the first bigger?'. Take one with no random choices, always doing the same on the same row. 3 numbers stand in 6 orders. Two orders moved alike end differently, so it must tell all 6 apart by its answers. Each answer doubles the possible answer lists.
So its worst row of 3, the order costing it most, takes at least 3 comparisons: the sorting floor for 3. 4 numbers stand in 4 × 3 × 2 × 1, 24 orders; 4 answers make 16 lists and 5 make 32, so the floor for 4 is 5. Four doublings of 1 make 16, so the log to base 2 of 16 is 4. 24 lies between 16 and 32, so its log lies between 4 and 5, about 4.58.
For numbers, from 2, the floor is , the log of the orders rounded up; is read factorial. It grows like (questions 4 and 5), times the log of , so every such sort's worst row grows at least that fast.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: the first place
the line:why it is allowed: Any of the numbers can stand first, so any of 3 for 1, 2 and 3.
the move: the second place
the line:first second third 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 The 6 orders of 1, 2 and 3, place by place. why it is allowed: From 2 numbers on, each first choice leaves for the second place. For 1, 2 and 3, 3 times 2 is 6, and the number left fills the third.
guess it, thenthe move: on to the last
the line:why it is allowed: Let be the number of orders. As in line 2, each place has one choice fewer, down to 1, and the choices multiply. For 3, 3 × 2 × 1 is 6.
guess it, thenthe move: n factorial
the line:why it is allowed: down to 1, multiplied, is written , read factorial.
guess it, thenthe move: read it
the line:why it is allowed: By line 3, one number more adds a factor. 3 numbers stand in 6 orders, 4 in 24 and 5 in 120.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: two answers
the line:why it is allowed: Here counts answers, not numbers. Each comparison answers yes or no.
the move: each answer doubles
the line:answers the lists how many 0 the empty list 1 1 yes; no 2 2 yes yes; yes no; no yes; no no 4 Each answer more doubles the lists, 1, 2, 4. why it is allowed: Each list goes on with a yes or a no, so each answer doubles the lists. No answers make one list, the empty one.
guess it, thenthe move: double n times
the line:why it is allowed: Let be the number of lists of answers. By line 2, 1 doubled times. For 3, 2 × 2 × 2 is 8.
guess it, thenthe move: a power
the line:why it is allowed: 2s multiplied are written , read 2 to the power . 2 to the power 0 is 1.
guess it, thenthe move: read it
the line:why it is allowed: By line 4, one answer more is one 2 more: 4 answers make 16 lists and 5 make 32.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: answers decide
the line:the row answers moves it ends as 1, 3, 2 no yes no swap the last two 1, 2, 3 10, 30, 20 no yes no swap the last two 10, 20, 30 Bubble sort walks left to right, asking each pair of neighbours 'is the left bigger?' and swapping on a yes. Each walk is one pair shorter, until one swaps nothing or no pair is left. why it is allowed: A comparing sort with no random choices learns only from its answers, so its next comparison and move hang only on its answers so far. So rows giving the same answers get the same comparisons and moves, and stop together.
the move: one list per order
the line:why it is allowed: Pretend two different orders of the same numbers gave the same answers. By line 1 they get the same moves, carrying what stands at each place to the same place on both. So they end different, at most one in order: a swap puts 2, 1 in order but not 1, 2. Yet the sort puts every row in order, so each order gets its own list.
guess it, thenthe move: fill out short lists
the line:Short lists carried down to 3 answers by dotted yeses. No no, for 1, 2, 3, begins no other list, so no no yes is new. why it is allowed: Let be the most comparisons on any order. Fill each shorter list out to answers with pretend yeses, so all have the one length line 4 counts. Filling adds only at the end. And no list begins another: if one did, by line 1 its row and the longer one's would stop together, so the longer would not go on. So the filled lists stay different.
guess it, thenthe move: count both
the line:orders the floor its lists 3 6 3 8 4 24 5 32 5 120 7 128 8 40,320 16 65,536 12 479,001,600 29 536,870,912 One answer fewer gives half the lists, too few. why it is allowed: Each answer doubles the lists, so answers make , against orders. By lines 2 and 3, lists are at least as many as orders. For 4, the 16 lists of 4 answers are too few for 24.
guess it, thenthe move: name the floor
the line:why it is allowed: Let be the fewest doublings of 1 that reach or pass it. For 4, four doublings reach only 16, short of 24, and the fifth, 32, passes it.
guess it, thenthe move: at least
the line:why it is allowed: By line 4, doublings of 1 reach , and by line 5 no fewer than do. So is at least .
guess it, thenthe move: halving and logarithms
the line:where does this come from?
question 4 of the page halving and logarithmsHalve , rounding up each time. How many halvings reach 1? the move: name the log rounded up
the line:why it is allowed: Here is the number halved, from 2 up. For 11, is 4.
the move: rounding up never takes off
the line:why it is allowed: The bracket is . For 11, 3.46 is at most 4.
the move: rounding up adds less than 1
the line:why it is allowed: For 11, 3 is below 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: As in line 4 of question 2 of the page halving and logarithms.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5 of question 2 of the page halving and logarithms. For 11, 11 is at most 16.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 8 is below 11.
the move: halve both sides of line 5
the line:why it is allowed: Halving both sides keeps it true.
the move: write the half as a power
the line:why it is allowed: As in line 9 of question 2 of the page halving and logarithms. For 11, 5.5 is at most 8.
the move: round the half up
the line:why it is allowed: is the half rounded up: the smallest whole number at or above it, 6 for 11. The right side, 8 for 11, is whole, as is at least 1. By line 9 it is at or above the half, so no smaller than the half rounded up.
the move: halve both sides of line 7
the line:why it is allowed: Halving both sides keeps it true.
the move: write the half as a power
the line:One half, a step left of 1, is 2 to the power minus 1. why it is allowed: As in line 9 of question 2 of the page halving and logarithms. For 11, 4 is below 5.5. For 2, is 1, so this is 2 to the power minus 1, one half, as drawn.
the move: round the half up
the line:why it is allowed: Rounding up never takes off. For 11, 4 is below 6.
the move: take the log of line 10
the line:why it is allowed: As in line 14 of question 2 of the page halving and logarithms.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 6, about 2.58, is at most 3.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 2, the log of one half is minus 1, below the log of 1, which is 0.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, 2 is below about 2.58.
the move: round the log up
the line:the number above at most its log rounded up 11 8 16 4 6 4 8 3 3 2 4 2 2 1 2 1 1 1/2 1 0 Each halving takes 1 off the log rounded up. why it is allowed: By lines 15 and 17, the log of the half is above two less than and at most one less, so it rounds up to one less. For 11, about 2.58 rounds up to 3.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded up is 1 or more, the log is above 0, so the number is above 1: at least 2. So line 18 takes 1 off each halving, and the number stays 2 or more until exactly halvings. For 11, 4, 3, 2, 1, 0.
the move: a log rounded up to 0 belongs to 1
the line:why it is allowed: Such a log is at most 0, so its number is at most 1, and so is 1.
the move: read lines 5 and 7 as doublings
the line:why it is allowed: Doubling 1 gives the powers of 2. By line 5, doublings get to or past ; by line 7, one fewer stay below. For 11, 8 stays below and 16 gets past.
the move: count merge sort's rounds
the line:Round 2 merges 5, 9 and 2, 7 into 2, 5, 7, 9. after round runs start 11 1 6 2 3 3 2 4 1 The first run holds 1, 2, 4, 8, then all 11: four rounds. The count of runs is 11 halved, rounding up. why it is allowed: Merge sort starts from runs of 1, whatever the row. Each round merges neighbouring runs in pairs, so the first run doubles, the last round taking only what is left. By line 21 it holds all after rounds, not fewer.
the move: read it
the line:why it is allowed: By lines 20 and 22, one count gives both the halvings and merge sort's rounds. For 11, four of each.
why it is allowed: The fewest doublings of 1 reaching a number from 2 up are its log to base 2 rounded up: 5 for 24. The box under this line, from the page halving and logarithms, shows it at its lines 5 and 7.
guess it, thenthe move: read it
the line:why it is allowed: By lines 6 and 7, every comparing sort that makes no random choices makes at least comparisons on its worst row of . It is only a floor: for 12 numbers it is 29, yet the best sort for 12 needs 30 on its worst row. An outside result, and no line here rests on it (Mark Wells, 1965; Knuth, The Art of Computer Programming, volume 3, section 5.3.1).
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
the move: name the log
the line:why it is allowed: Let be the log to base 2 of , for from 4 as lines 12 and 15 need. For 8, about 15.30.
the move: factors at most n
the line:why it is allowed: multiplies whole numbers, each at most . Putting for each gives , read to the power , no smaller. For 8, 40,320 against 16,777,216.
guess it, thenthe move: bigger number, bigger log
the line:why it is allowed: On a number line of powers of 2, 16 stands right of 8, and its log, 4, right of 8's log, 3. Any two numbers go the same way, as the page halving and logarithms shows, so line 2 holds for the logs.
guess it, thenthe move: into line 1
the line:why it is allowed: By line 3, as is the log of .
guess it, thenthe move: log of a power
the line:why it is allowed: For 8: 8 is three 2s, so 8 to the power 8 is 24 2s, since multiplying powers of 2 adds the powers. Each number is 2 raised to its log, so the log of a product adds the logs. is a product of factors, each , so its log is times .
guess it, thenthe move: keep the top half
the line:why it is allowed: Drop the factors 1 to half of rounded down; each is at least 1, so the product gets no bigger. Half of rounded down and rounded up add up to , as 3 and 4 for 7. So are kept, read half of rounded up.
guess it, thenthe move: half of n for each
the line:for 8 multiplied 8 down to 1 40,320 8 × 7 × 6 × 5 1,680 4 × 4 × 4 × 4 256 Drop 4 down to 1, then put 4, half of 8, for each factor kept. why it is allowed: Each kept factor is above half of , so putting half of for each gives no bigger product.
guess it, thenthe move: bigger number, bigger log
the line:why it is allowed: As in line 3.
guess it, thenthe move: into line 1
the line:why it is allowed: By line 8, as is the log of .
guess it, thenthe move: log of a power
the line:why it is allowed: As in line 5. For 8, 4 times 2.
guess it, thenthe move: rounding up
the line:why it is allowed: Rounding up makes no smaller, as 3.5 goes up to 4.
guess it, thenthe move: log of half of n
the line:why it is allowed: From 4 on, half of is at least 2, whose log is 1; by line 3, bigger numbers have bigger logs.
guess it, thenthe move: drop the rounding
the line:why it is allowed: By line 11, half of is at most , as 3.5 against 4 for 7. Each multiplies the log of half of , above zero by line 12, so putting half of in line 10 makes it no bigger.
guess it, thenthe move: halving takes 1 off
the line:why it is allowed: is 2 times half of , and the log of 2 is 1. So by line 5 the log of half of is . For 8, the log of 4 is 3 less 1.
guess it, thenthe move: a fact about n
the line:why it is allowed: From 4 on, is at least 2, so half of it is at least 1, and taking 1 away leaves at least half. For 8, 1.5 against 2.
guess it, thenthe move: the smaller part
the line:why it is allowed: By line 15, putting half of for in line 14 makes it no bigger, as in line 13. For 8, 4 times 1.5, 6.
guess it, thenthe move: multiply out
the line:- the log of
- a quarter of
The log of between the other two, from 1 to 10 numbers. At 8: 24, about 15.30, and 6. why it is allowed: Half of times half of .
guess it, thenthe move: a ceiling
the line:why it is allowed: , read is in big O of , says a fixed multiple of it is a ceiling of . By line 5, 1 times it, from 4 on.
guess it, thenthe move: a floor
the line:why it is allowed: , read big omega, says a fixed multiple, above zero, is a floor of , a floor on growth, not the sorting floor. By line 17, a quarter, from 4 on.
guess it, thenthe move: both
the line:why it is allowed: , read big theta, says both, by lines 18 and 19. grows like .
guess it, then
try it with a small : at , line 1 gives , about 15.3
the move: name the floor
the line:why it is allowed: Let be the floor of question 3, for from 4 as line 3 needs. For 8, 16.
the move: rounding up
the line:why it is allowed: Rounding up makes no smaller. For 8, 16 against about 15.30.
guess it, thenthe move: question 4's floor
the line:why it is allowed: By line 17 of question 4. For 8, 6.
guess it, thenthe move: adds less than 1
the line:why it is allowed: Rounding up adds less than 1. For 8, about 16.30.
guess it, thenthe move: question 4's ceiling
the line:why it is allowed: By line 5 of question 4. For 8, 25.
guess it, thenthe move: a fact about n
the line:why it is allowed: From 4 on, is at least 2, so is at least 8.
guess it, thenthe move: put it for the 1
the line:why it is allowed: By line 6, this makes the right side of line 5 no smaller.
guess it, thenthe move: add them
the line:why it is allowed: For 8, 48.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 8, as in line 18 of question 4, the multiple 2.
guess it, thenthe move: a floor
the line:why it is allowed: By line 3, as in line 19 of question 4, the multiple a quarter.
guess it, thenthe move: both
the line:why it is allowed: By lines 9 and 10. By lines 3 and 8, from 4 on, lies between a quarter of and twice it.
guess it, thenthe move: the worst row
the line:the floor merge sort bubble sort 4 5 5 6 5 7 9 10 6 10 11 15 7 13 14 21 Two of this site's sorts, their most comparisons over every order of . Neither is below the floor. why it is allowed: is the most comparisons a comparing sort with no random choices makes on any order of , at least by lines 6 and 7 of question 3.
guess it, thenthe move: put line 3 in
the line:why it is allowed: By lines 3 and 12. For 8, 16 against 6.
guess it, thenthe move: every worst row
the line:why it is allowed: Line 13 holds for every comparing sort with no random choices, so is a floor on the growth of every such worst row.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Find the floor for 6 numbers.
- By question 1, 6 numbers stand in 6 × 5 × 4 × 3 × 2 × 1 orders, 720.
- By question 2, 9 answers make 512 lists, too few, and 10 make 1,024.
- By question 3, a comparing sort with no random choices takes, on its worst row of 6, at least 10 comparisons: the log of 720, about 9.49, rounded up.
the trap
check yourself
In how many orders can 5 different numbers stand?
the answer
By question 1, 5 times 4 times 3 times 2 times 1, which is 120.
At least how many comparisons does a comparing sort with no random choices make on its worst row of 5?
the answer
By question 3, 7. 6 give 64 lists, too few for 120 orders, and 7 give 128.
Can a comparing sort with no random choices put every row of 8 in order in at most 15 comparisons?
the answer
No. 15 give 32,768 lists, too few for 40,320 orders, so by question 3 its worst row takes at least 16.