Skip to content

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.

depth012393928619856029418359192836495865768299104111128133145150123
Halving on 6, 2, 9, 4, 1, 8, 3, 5. Each call on 2 or more numbers starts the two under it, then keeps the bigger answer. While the 1st number, 6, answers, the 4 marked calls are held. The squares list the calls by level.

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 onit answers
6, 2, 9, 49
2, 9, 49
9, 49
44
Taking one off. The answers come back up: 4, 9, 9, 9.
waycalls madecomparisonsmost calls held
halving1574
taking one off878
walking along171
Three ways on 8 numbers. In halving, each of the 7 calls that split compares once.

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 nn numbers holds at most ⌈log⁡2 n⌉+1\lceil \log_2\, n \rceil + 1 calls at once, read the log to base 2 of nn, 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.

numberlog× 2+ 11021428372.81
One step right doubles the number and adds 1 to its log. 7's log is about 2.81.

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.

82614pivot: the last number82614not smaller: stays28614smaller: swapped left28614not smaller: stays21684smaller: swapped left21486pivot placed
Quick sort's split of 8, 2, 6, 1, 4, a row a step: each number against the pivot 4.

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.

4321the row3214after walk 13 pairs compared2134after walk 22 pairs compared1234after walk 31 pair comparedone walkin place for good
Bubble sort on 4, 3, 2, 1, a worst order. Each walk carries the biggest still unplaced to its place.
the row: 1, 3, 2, 41111start22round 14round 22a run, its length insidethe first runall in one runa merge in round 2, its numbers13241234goes straight in, the other run used up
Merge sort on 1, 3, 2, 4, a worst order. Round 1 compares twice, round 2 three times: 1 with 2, 3 with 2 and 3 with 4.
numbersordersbubble sort, worstmerge sort, worstmerge sort compares less on
22110
36332
4246519
75,04021144,976
Comparisons, with both sorts run on every order.

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 nn, 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.

question 1Find the biggest of nn numbers by halving. How many calls does it make?
  1. the move: a call is on one number or splits

    the line:
    each call is on 1 number,or splits its numbers in two\begin{gathered} \text{each call is on 1 number,} \\ \text{or splits its numbers in two} \end{gathered}

    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.

  2. the move: count the calls on one number

    the line:
    n calls areon one number each\begin{gathered} n \text{ calls are} \\ \text{on one number each} \end{gathered}

    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.

  3. the move: each split makes one part more

    the line:
    each split turns1 part into 2\begin{gathered} \text{each split turns} \\ \text{1 part into 2} \end{gathered}

    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.

  4. the move: count the splits

    the line:
    n−1 callssplit\begin{gathered} n - 1 \text{ calls} \\ \text{split} \end{gathered}
    levelcallsnumbers in eachcalls that split
    0181
    1242
    2424
    3810
    8 calls on one number, 7 that split: 15 in all.

    why it is allowed: By line 2 the parts end as nn single numbers. By line 3 each split adds one part, so going from 1 part to nn takes n−1n - 1 splits. For 8 numbers, 1 + 2 + 4, which is 7.

  5. the move: add the two kinds of call

    the line:
    K=n+(n−1)K = n + (n - 1)

    why it is allowed: Let KK 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.

  6. the move: tidy it

    the line:
    K=2n−1K = 2n - 1

    why it is allowed: nn and nn make 2n2n, less 1. For 8, 16 less 1 is 15.

  7. the move: read it

    the line:
    one call fewer thantwice the numbers\begin{gathered} \text{one call fewer than} \\ \text{twice the numbers} \end{gathered}

    why it is allowed: By line 6, 64 numbers take 127 calls, and 4,096 take 8,191.

try it with a small nn: at n=8n = 8, line 5 gives K=15K = 15, which you can work out by hand

question 2Find the biggest of nn numbers by halving. At most how many calls are held at once?
  1. the move: the calls held form a chain

    the line:
    the calls held run from the firstdown to the one now working\begin{gathered} \text{the calls held run from the first} \\ \text{down to the one now working} \end{gathered}
    depth0123734232122110111117142332425261718191101111121130123
    Each circle a call and its count of numbers; the table's first row is marked.
    while the call on this answersthe calls held, from the topheld
    the 1st numberall 7; 1st to 4th; 1st and 2nd; 1st4
    the 4th numberall 7; 1st to 4th; 3rd and 4th; 4th4
    the 7th numberall 7; 5th to 7th; 7th3
    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.

  2. the move: the most held is a longest path down to one number

    the line:
    the most held is the lengthof a longest path downto a call on one number\begin{gathered} \text{the most held is the length} \\ \text{of a longest path down} \\ \text{to a call on one number} \end{gathered}

    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.

  3. the move: the left part every time is a longest path

    the line:
    the all-left path, halving n,rounding up, down to 1,is a longest\begin{gathered} \text{the all-left path, halving } n \text{,} \\ \text{rounding up, down to 1,} \\ \text{is a longest} \end{gathered}
    rounding down73.5 → 31.5 → 12 halvingsrounding up73.5 → 4213 halvings
    7 halved to 1: down as right parts do, up as the all-left path does.
    the path from the toppaths like itcalls
    7, 4, 2, 144
    7, 3, 2, 124
    7, 3, 113
    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 nn, any path's part is at most the all-left path's part, and any path reaches 1 no later.

  4. the move: count the calls in it

    the line:
    D=the halvings, rounding up,from n to 1, plus 1\begin{gathered} D = \text{the halvings, rounding up,} \\ \text{from } n \text{ to 1, plus 1} \end{gathered}

    why it is allowed: Let DD 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.

  5. the move: use the page on halving and logarithms

    the line:
    D=⌈log⁡2 n⌉+1D = \lceil \log_2\, n \rceil + 1
    where does this come from?
    question 4 of the page halving and logarithmsHalve nn, rounding up each time. How many halvings reach 1?
    1. the move: name the log rounded up

      the line:
      C=⌈log⁡2 n⌉C = \lceil \log_2\, n \rceil

      why it is allowed: Here nn is the number halved, from 2 up. For 11, CC is 4.

    2. the move: rounding up never takes off

      the line:
      log⁡2 n≤⌈log⁡2 n⌉\log_2\, n \le \lceil \log_2\, n \rceil

      why it is allowed: The bracket is CC. For 11, 3.46 is at most 4.

    3. the move: rounding up adds less than 1

      the line:
      ⌈log⁡2 n⌉−1<log⁡2 n\lceil \log_2\, n \rceil - 1 < \log_2\, n

      why it is allowed: For 11, 3 is below 3.46.

    4. the move: raise 2 to each side of line 2

      the line:
      2log⁡2 n≤2⌈log⁡2 n⌉2^{\log_2\, n} \le 2^{\lceil \log_2\, n \rceil}

      why it is allowed: As in line 4 of question 2 of the page halving and logarithms.

    5. the move: 2 to the log of n is n

      the line:
      n≤2⌈log⁡2 n⌉n \le 2^{\lceil \log_2\, n \rceil}

      why it is allowed: As in line 5 of question 2 of the page halving and logarithms. For 11, 11 is at most 16.

    6. the move: raise 2 to each side of line 3

      the line:
      2⌈log⁡2 n⌉−1<2log⁡2 n2^{\lceil \log_2\, n \rceil - 1} < 2^{\log_2\, n}

      why it is allowed: As in line 4.

    7. the move: 2 to the log of n is n

      the line:
      2⌈log⁡2 n⌉−1<n2^{\lceil \log_2\, n \rceil - 1} < n

      why it is allowed: As in line 5. For 11, 8 is below 11.

    8. the move: halve both sides of line 5

      the line:
      n/2≤2⌈log⁡2 n⌉/2n / 2 \le 2^{\lceil \log_2\, n \rceil} / 2

      why it is allowed: Halving both sides keeps it true.

    9. the move: write the half as a power

      the line:
      n/2≤2⌈log⁡2 n⌉−1n / 2 \le 2^{\lceil \log_2\, n \rceil - 1}

      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.

    10. the move: round the half up

      the line:
      ⌈n/2⌉≤2⌈log⁡2 n⌉−1\lceil n / 2 \rceil \le 2^{\lceil \log_2\, n \rceil - 1}

      why it is allowed: ⌈n/2⌉\lceil n / 2 \rceil 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 CC is at least 1. By line 9 it is at or above the half, so no smaller than the half rounded up.

    11. the move: halve both sides of line 7

      the line:
      2⌈log⁡2 n⌉−1/2<n/22^{\lceil \log_2\, n \rceil - 1} / 2 < n / 2

      why it is allowed: Halving both sides keeps it true.

    12. the move: write the half as a power

      the line:
      2⌈log⁡2 n⌉−2<n/22^{\lceil \log_2\, n \rceil - 2} < n / 2
      numberlog× 2+ 11/2−1102142
      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, CC is 1, so this is 2 to the power minus 1, one half, as drawn.

    13. the move: round the half up

      the line:
      2⌈log⁡2 n⌉−2<⌈n/2⌉2^{\lceil \log_2\, n \rceil - 2} < \lceil n / 2 \rceil

      why it is allowed: Rounding up never takes off. For 11, 4 is below 6.

    14. the move: take the log of line 10

      the line:
      log⁡2 ⌈n/2⌉≤log⁡2 2⌈log⁡2 n⌉−1\log_2\, \lceil n / 2 \rceil \le \log_2\, 2^{\lceil \log_2\, n \rceil - 1}

      why it is allowed: As in line 14 of question 2 of the page halving and logarithms.

    15. the move: the log of a power of 2 is the power

      the line:
      log⁡2 ⌈n/2⌉≤⌈log⁡2 n⌉−1\log_2\, \lceil n / 2 \rceil \le \lceil \log_2\, n \rceil - 1

      why it is allowed: For 11, the log of 6, about 2.58, is at most 3.

    16. the move: take the log of line 13

      the line:
      log⁡2 2⌈log⁡2 n⌉−2<log⁡2 ⌈n/2⌉\log_2\, 2^{\lceil \log_2\, n \rceil - 2} < \log_2\, \lceil n / 2 \rceil

      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.

    17. the move: the log of a power of 2 is the power

      the line:
      ⌈log⁡2 n⌉−2<log⁡2 ⌈n/2⌉\lceil \log_2\, n \rceil - 2 < \log_2\, \lceil n / 2 \rceil

      why it is allowed: For 11, 2 is below about 2.58.

    18. the move: round the log up

      the line:
      ⌈log⁡2 ⌈n/2⌉⌉=⌈log⁡2 n⌉−1\lceil \log_2\, \lceil n / 2 \rceil \rceil = \lceil \log_2\, n \rceil - 1
      the numberaboveat mostits log rounded up
      118164
      6483
      3242
      2121
      11/210
      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 CC and at most one less, so it rounds up to one less. For 11, about 2.58 rounds up to 3.

    19. the move: use line 18 again and again

      the line:
      after C halvings,rounding up, the logrounded up is 0\begin{gathered} \text{after } C \text{ halvings,} \\ \text{rounding up, the log} \\ \text{rounded up is 0} \end{gathered}

      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 CC halvings. For 11, 4, 3, 2, 1, 0.

    20. the move: a log rounded up to 0 belongs to 1

      the line:
      C halvings, rounding up,bring n to 1\begin{gathered} C \text{ halvings, rounding up,} \\ \text{bring } n \text{ to 1} \end{gathered}

      why it is allowed: Such a log is at most 0, so its number is at most 1, and so is 1.

    21. the move: read lines 5 and 7 as doublings

      the line:
      C doublings of 1 reach n,one fewer do not\begin{gathered} C \text{ doublings of 1 reach } n, \\ \text{one fewer do not} \end{gathered}

      why it is allowed: Doubling 1 gives the powers of 2. By line 5, CC doublings get to or past nn; by line 7, one fewer stay below. For 11, 8 stays below and 16 gets past.

    22. the move: count merge sort's rounds

      the line:
      merge sort on this sitemakes C rounds\begin{gathered} \text{merge sort on this site} \\ \text{makes } C \text{ rounds} \end{gathered}
      the row: 5, 9, 2, 7, 11, 1, 8, 3, 10, 6, 411111111111start222221round 1443round 283round 311round 42a run, its length insideno partner, carried downthe first runall in one runa merge in round 2, its numbers59272579goes straight in, the other run used up
      Round 2 merges 5, 9 and 2, 7 into 2, 5, 7, 9.
      after roundruns
      start11
      16
      23
      32
      41
      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 nn after CC rounds, not fewer.

    23. the move: read it

      the line:
      C halvings,rounding up, andC rounds of merge sort\begin{gathered} C \text{ halvings,} \\ \text{rounding up, and} \\ C \text{ rounds of merge sort} \end{gathered}

      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.

    the whole page: halving and logarithms

    why it is allowed: The working under 'where does this come from?' counts these halvings for nn from 2: the log to base 2 of nn, 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 DD is 3 + 1, which is 4. For 1 number, no halving: DD is 1.

  6. the move: double the numbers, by lines 3 and 4

    the line:
    one more call heldeach time n doubles\begin{gathered} \text{one more call held} \\ \text{each time } n \text{ doubles} \end{gathered}

    why it is allowed: Half of twice nn, rounded up, is nn. So by lines 3 and 4, the all-left path from twice nn, 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.

try it with a small nn: at n=7n = 7, line 4 gives D=4D = 4, which you can work out by hand

question 3Find the biggest of nn numbers by taking one off. How many calls does it make, and how many are held at once?
  1. the move: each call takes off one number

    the line:
    each call is on one number morethan the call it starts\begin{gathered} \text{each call is on one number more} \\ \text{than the call it starts} \end{gathered}
    the momenthow many numbers each held call is onheld
    start on 441
    start on 34, 32
    start on 24, 3, 23
    answer the last4, 3, 2, 14
    keep the bigger of 24, 3, 23
    keep the biggest of 441
    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.

  2. the move: count the calls

    the line:
    T=nT = n
    numbershalving, calls madehalving, most heldtaking one off, calls made and held
    81548
    64127764
    5121,02310512
    4,0968,191134,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 TT be the number of calls. By line 1 there is one for each count from nn down to 1, which is nn calls. For 4 numbers, 4.

  3. the move: every call is held when the last one starts

    the line:
    all n calls are heldwhen the last one starts\begin{gathered} \text{all } n \text{ calls are held} \\ \text{when the last one starts} \end{gathered}

    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 TT at once. For 4 numbers, the table at line 1 climbs to 4.

try it with a small nn: at n=4n = 4, line 2 gives T=4T = 4, which you can work out by hand

a worked example

Halving on 5 numbers: count its calls and the most it holds at once.

  1. 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.
  2. 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

  1. How many calls does halving make on 16 numbers?

    the answer

    By question 1, 2 times 16, less 1, which is 31.

  2. 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.

  3. 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.