split, solve and combine (divide and conquer)
why it works
what it is
To find the biggest of 8 numbers, split them into two lots of 4, find the biggest of each the same way, and keep the bigger. This is split, solve and combine, or divide and conquer. Here it makes 15 calls, but holds at most 4 at once.
Take 6, 2, 9, 4, 1, 8, 3, 5. Split them into a left 4 and a right 4. Solve each part the same way, down to single numbers: one number is its own biggest. Combine by keeping the bigger of the two answers. An odd count gives the left part the extra one: 5 splits into 3 and 2. Call this way halving. Each use of it on one part is a call. A call started and not yet answered is held, kept in memory. The computer works on one call at a time, and the left part's call answers before the right part's starts.
Halving has two costs: the calls it makes, its work, and the calls it holds at once, its memory, since each held call takes the same small room. Taking one off, in question 3, hands on all but the first number, as the first table shows. Walking along the row keeps the biggest so far, starting no other call. The second table counts all three on 8 numbers.
| the call is on | it answers |
|---|---|
| 6, 2, 9, 4 | 9 |
| 2, 9, 4 | 9 |
| 9, 4 | 9 |
| 4 | 4 |
| way | calls made | comparisons | most calls held |
|---|---|---|---|
| halving | 15 | 7 | 4 |
| taking one off | 8 | 7 | 8 |
| walking along | 1 | 7 | 1 |
The calls for 8 numbers are the first call and the calls for 4, twice. Each half of the tree has 7 calls, so that is 1 + 7 + 7, which is 15. A cost given like this, by the same cost on smaller jobs, is called a recurrence. Unrolling a recurrence writes it out level by level. Each 7 is its half's 1 + 2 + 4. So 1 + 7 + 7 is 1 + (1 + 1) + (2 + 2) + (4 + 4), the squares' 1 + 2 + 4 + 8. The page 'recurrences unrolled' uses both words too, and unrolls merge sort that way, counting its placings, a placing being one number put into a merged run.
Question 2 finds that halving on numbers holds at most calls at once, read the log to base 2 of , rounded up, plus 1. 2 × 2 × 2 is 8, three 2s, so the log of 8 is 3, and halving on 8 numbers holds at most 4. 7 lies between 4 and 8, so its log lies between 2 and 3. The bent brackets round it up to 3.
Finding the biggest compares only in the combine. So does merge sort, which puts a row in order: all its work is merging two runs in order into one. This site's merge sort skips the cutting: it starts from single numbers and merges them in rounds. Quick sort compares only in the split. On 8, 2, 6, 1, 4 it takes the last number, 4, as its pivot, and moves 2 and 1 to its left. Sort each side, and nothing is left to combine.
Splitting does save work on some jobs, and sorting is one. Bubble sort walks along the row, swapping each neighbouring pair that stands the wrong way round, and stops after a walk with no swap. On 4, 3, 2, 1 each walk is one pair shorter: 3 + 2 + 1 = 6 comparisons. Merge sort moves the smaller front number of two runs across, and once one run is used up, the rest moves free. On 1, 3, 2, 4 it makes 5.
| numbers | orders | bubble sort, worst | merge sort, worst | merge sort compares less on |
|---|---|---|---|---|
| 2 | 2 | 1 | 1 | 0 |
| 3 | 6 | 3 | 3 | 2 |
| 4 | 24 | 6 | 5 | 19 |
| 7 | 5,040 | 21 | 14 | 4,976 |
4 different numbers stand in 4 × 3 × 2 × 1 = 24 orders: any of the 4 first, any of the 3 left next, and so on. 7 stand in 5,040 the same way. On 4 and 7 numbers, merge sort's worst costs less, and it compares less on most orders. Not on all: on 1 to 7 in order, bubble sort makes one walk of 6 comparisons and stops, while merge sort makes 11.
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: a call is on one number or splits
the line:why it is allowed: A call on one number answers with it. A call on 2 numbers or more splits them into two parts, each of 1 number or more. So no call is on 0 numbers.
the move: count the calls on one number
the line:why it is allowed: The two parts of a split share no number and leave none out. So each number ends in exactly one call on it alone. For 8 numbers, 8 such calls.
guess it, thenthe move: each split makes one part more
the line:why it is allowed: A split cuts one part into two, so each split adds one part. Before any split the row is 1 part. For 8 numbers, 2 parts after 1 split, 3 after 2, and 8 after 7.
guess it, thenthe move: count the splits
the line:level calls numbers in each calls that split 0 1 8 1 1 2 4 2 2 4 2 4 3 8 1 0 8 calls on one number, 7 that split: 15 in all. why it is allowed: By line 2 the parts end as single numbers. By line 3 each split adds one part, so going from 1 part to takes splits. For 8 numbers, 1 + 2 + 4, which is 7.
guess it, thenthe move: add the two kinds of call
the line:why it is allowed: Let be the number of calls. By line 1 each is on one number or splits, and lines 2 and 4 count each kind. For 8 numbers, 8 + 7, which is 15.
guess it, thenthe move: tidy it
the line:why it is allowed: and make , less 1. For 8, 16 less 1 is 15.
guess it, thenthe move: read it
the line:why it is allowed: By line 6, 64 numbers take 127 calls, and 4,096 take 8,191.
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
the move: the calls held form a chain
the line:Each circle a call and its count of numbers; the table's first row is marked. while the call on this answers the calls held, from the top held the 1st number all 7; 1st to 4th; 1st and 2nd; 1st 4 the 4th number all 7; 1st to 4th; 3rd and 4th; 4th 4 the 7th number all 7; 5th to 7th; 7th 3 Three moments on 7 numbers. why it is allowed: A call answers only after both its calls have, so the call that started a held call is held too. A call starts its right part only after its left part answers, so it never has two calls held. Climb from the call now working, each step to the call that started it. Every call met on the way up is held. Another held call's climb meets this one at some call, which would then have two calls held, or be the working call, waiting. The table shows three such chains on 7.
the move: the most held is a longest path down to one number
the line:why it is allowed: A path runs from the first call down to a call on one number, each call on a part of the one above. Its length is how many calls it has. A chain is the top part of a path, held at one moment. While the call at a path's bottom works, its climb by line 1 holds the whole path. A chain whose last call is on 2 numbers or more grows. At some moment that call waits on its left part, held below it, and so on down. So the most held is the length of a longest path. On 7, the chain 7, 4, 2 grows to 7, 4, 2, 1.
guess it, thenthe move: the left part every time is a longest path
the line:7 halved to 1: down as right parts do, up as the all-left path does. the path from the top paths like it calls 7, 4, 2, 1 4 4 7, 3, 2, 1 2 4 7, 3, 1 1 3 Every path on 7, top to bottom. why it is allowed: On any path each part is half the one before, rounded up for a left part and down for a right part. The all-left path takes the left part every time. Take two counts, the first at most the second, such as 3 and 4. Halved and rounded up, they give 2 and 2; 3 rounded down gives 1. Half the second, rounded up, is a whole number at or above half the second, so at or above half the first. Half the first, rounded up, is the smallest such whole number, and rounded down it is no more. So, step by step from , any path's part is at most the all-left path's part, and any path reaches 1 no later.
guess it, thenthe move: count the calls in it
the line:why it is allowed: Let be the most calls held at once. By lines 2 and 3 it is the length of the all-left path: a call for each halving, and one for the first. For 7, 3 halvings, so 4 calls.
guess it, thenthe move: use the page on 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 working under 'where does this come from?' counts these halvings for from 2: the log to base 2 of , rounded up. Its lines 19 to 21 count them, from its lines 1 to 18, and its lines 22 and 23 can be skipped. For 7, 3 doublings of 1 first pass 7, so is 3 + 1, which is 4. For 1 number, no halving: is 1.
guess it, thenthe move: double the numbers, by lines 3 and 4
the line:why it is allowed: Half of twice , rounded up, is . So by lines 3 and 4, the all-left path from twice , and the most held, is one call longer. By line 5, halving holds at most 4 calls on 7 numbers, 5 on 14 and 6 on 28.
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: each call takes off one number
the line:the moment how many numbers each held call is on held start on 4 4 1 start on 3 4, 3 2 start on 2 4, 3, 2 3 answer the last 4, 3, 2, 1 4 keep the bigger of 2 4, 3, 2 3 keep the biggest of 4 4 1 The calls held climb to 4, then fall. why it is allowed: A call on one number answers with it. A call on more numbers calls itself on all but the first, then keeps the bigger of the first and that answer. On 4 numbers the calls are on 4, 3, 2 and 1.
the move: count the calls
the line:numbers halving, calls made halving, most held taking one off, calls made and held 8 15 4 8 64 127 7 64 512 1,023 10 512 4,096 8,191 13 4,096 Halving holds 3 more from 8 to 64 numbers, 3 doublings. Taking one off holds all its calls, by line 3. why it is allowed: Let be the number of calls. By line 1 there is one for each count from down to 1, which is calls. For 4 numbers, 4.
guess it, thenthe move: every call is held when the last one starts
the line:why it is allowed: No call answers before the call it started. So when the call on the last number starts, all the calls before it are waiting, and it is held too: all at once. For 4 numbers, the table at line 1 climbs to 4.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
a worked example
Halving on 5 numbers: count its calls and the most it holds at once.
- 5 splits into 3 and 2, the 3 into 2 and 1, and each 2 into 1 and 1: 9 calls. By question 1, 2 times 5, less 1.
- The all-left path 5, 3, 2, 1 holds 4 calls. By question 2, 3 doublings of 1 first pass 5, and 3 plus 1 is 4.
the trap
check yourself
How many calls does halving make on 16 numbers?
the answer
By question 1, 2 times 16, less 1, which is 31.
At most how many calls does halving hold at once on 16 numbers?
the answer
2 × 2 × 2 × 2 is 16, so its log is 4. By question 2, 4 plus 1 is 5.
On 100 numbers, which holds fewer calls at once, halving or taking one off?
the answer
Halving. 7 doublings of 1 first pass 100: 2, 4, 8, 16, 32, 64, 128. So by question 2 it holds at most 8. Taking one off holds 100, by question 3.