Divide and Conquer [00PJ]
- October 19, 2026
The next standard problem-solving strategy we will consider is divide and conquer.
Divide and conquer introduction [00PK]
- October 19, 2026
Divide and conquer is a typical problem solving strategy that works as follows:
- Break up the input into (smaller) subproblems.
- Solve the subproblems recursively.
- Combine the subproblem solutions into an overall solution.
- To implement a divide-and-conquer strategy, we can use recursion.
- To prove the correctness of a divide-and-conquer strategy, we can use proof by induction.
- To analyze the asymptotic runtime of a divide-and-conquer strategy, we can use recurrence relations and the Master Theorem.
Divide and conquer example: Mergesort [00PL]
- October 19, 2026
The material in this section is presented via a POGIL activity.
A classic example of a divide-and-conquer algorithm is mergesort, which works by splitting the input list into two halves, recursively sorting each half, and then merging the sorted halves together.
Algorithm Mergesort [00PP]
- October 19, 2026
Mergesort analysis [00PQ]
- October 19, 2026
How long does mergesort take on a list of length n? Looking at the code above, we can see that
- It takes \Theta (1) in the base case (a list of length \leq 1).
- Otherwise, it makes two recursive calls to itself on lists of length approximately n/2.
- After the recursive calls complete, it does \Theta (n) additional work to merge the two sorted lists into a single sorted list.
If we draw out a tree showing all the recursive calls of \mathsf {mergesort} on an initial input of length n, it looks something like this:
The expression at each node indicates both the size of the input to that recursive call, as well as the amount of work which happens at that node (since the amount of work at each node is linear). We can see that- The tree has height \lg n, since we divide the input in half at each level until reaching a base case of 1.
- There is n total work happening at each level: n = n/2 + n/2 = n/4 + n/4 + n/4 + n/4 = \dots .
Divide and conquer example: multiplication [00PR]
- October 21, 2026
The material in this section is presented via a POGIL activity.
Another classic place where divide-and-conquer comes up is in trying to multiply large numbers faster than the naïve algorithm you learned in grade school.
Motivation [00PS]
- October 21, 2026
In many situations, when we use arithmetic operations on integers such as addition or multiplication, we simply assume they take constant time. This is appropriate when the integers are bounded by some fixed size, e.g. if all the integers are 64-bit integers. However, if we want to be able to deal with integers of arbitrary size, this is no longer appropriate; we must take the size of the integers into account. For example, as of October 20, 2026, the largest known prime number is 2^{136\,279\,841} - 1, a number with 41\,024\,320 decimal digits. The arithmetic operations needed to test this number for primality take a nontrivial amount of time!
n-bit addition [00PT]
- October 22, 2026
Adding two n-bit numbers is easily analyzed: we iterate through, starting with the ones place, and add the bits in each place (possibly doing another addition for a carry bit), for a total of about 2n bit operations in the worst case. Hence we know it is possible to add two n-bit numbers in linear time, so the worst-case time to add is O(n). However, it is also clearly \Omega (n), since we have to at least look at every bit in the input. Hence, adding n-bit numbers takes \Theta (n) time.
Naïve n-bit multiplication [00PU]
- October 22, 2026
What about multiplication? Using the standard "grade school" algorithm to compute p \times q, we end up with a shifted copy of p for every 1 bit in q, and then we must add then all. So there are up to n shifted copies of p to add, with each being at most 2n bits, so this particular algorithm will take \Theta (n^2) in the worst case.
However, unlike addition which obviously takes \Omega (n) time, it is not so obvious that multiplication must take \Omega (n^2) time. In fact, if anything, it is the opposite: it seems like adding up a bunch of shifted copies of the same bitstring might involve some redundant work. Perhaps there is a way we can multiply two n-bit numbers in faster than quadratic time.
Divide and conquer multiplication [00PV]
- October 22, 2026
Given an n-bit number X (and assuming n is even), we can split X into two halves X_1 and X_2, each with n/2 bits. We can express this as X = 2^{n/2} X_1 + X_2 since multiplying by a power of two performs a bit shift, i.e. adding zeros to the end of a number in binary (just like multiplying by a power of ten adds zeros to the end of a number in decimal—and for the same reason).
Remark What if n is odd?
- October 22, 2026
Note that we can still do something similar if n is odd—for example we can write X = 2^{\lceil n/2 \rceil } X_1 + X_2 where X_2 is one digit longer than X_1 (or we can imagine "padding" X_1 to length \lceil n/2 \rceil by adding a zero digit on the left).
If we have two n-bit numbers expressed in this form and multiply them, we get AB = (2^{n/2} A_1 + A_2)(2^{n/2} B_1 + B_2) = 2^n A_1 B_1 + 2^{n/2}(A_1 B_2 + A_2 B_1) + A_2 B_2. On the left-hand side we have a single n-bit multiplication; on the right-hand side we have four n/2-bit multiplications, two shifts, and three n-bit additions. We can turn this into a recursive algorithm for computing the product AB, something like the following:
(This works when n is a power of two; it is not hard (though a bit fiddly) to generalize to any value of n.)
Analysis of naïve divide-and-conquer multiplication [00PX]
- October 23, 2026
This divide-and-conquer multiplication algorithm involves four recursive calls to \mathsf {mul} on inputs of size n/2, two shifts (which take constant time), and three n-bit additions (which take \Theta (n)). Hence we can write a recurrence for the time M(n) taken by \mathsf {mul} on n-bit inputs: \begin {align*} M(1) &= \Theta (1) \\ M(n) &= 4M(n/2) + \Theta (n) \end {align*} But what is a closed form for M(n)? Is it better than n^2?
We can unroll this recurrence into a recursion tree to figure out how much total work happens.
- At the root, an amount of work happens proportional to the input size, n.
- At the first level of recursion, there are four recursive calls, each on n/2-bit numbers, giving a total something like 4 \cdot (n/2) = 2n.
- At the second level, there are sixteen recursive calls on n/4-bit inputs, giving a total of 4n.
- In general, at depth k, there are 4^k recursive calls, each on an input of size n/2^k, for a total of 4^k \cdot n/2^k = 2^k n.
The Master Theorem [00PY]
- October 23, 2026
Adding up the total work in a recursion tree for divide-and-conquer algorithms can be generalized to a powerful theorem known as the Master Theorem. (It is not named for someone; it is just a powerful theorem that "masters" a very wide range of divide-and-conquer algorithms. This is especially true when considering its generalizations such as the Akra-Bazzi method.)
Theorem Master Theorem [00Q1]
- October 23, 2026
If T(n) = a T(n/b) + \Theta (n^d) for positive constants a, b, and d, then T(n) = \begin {cases} \Theta (n^d) & \text {if } a < b^d \\ \Theta (n^d \log n) & \text {if } a = b^d \\ \Theta (n^{\log _b a}) & \text {if } a > b^d. \end {cases}
Remark Master theorem intuition [00Q5]
- October 23, 2026
Intuitively, a tells us how fast the number of recursive calls grows; b^d tells us how fast the problems are getting smaller. The ratio of these will end up being the common ratio of a geometric sum.
- If a < b^d, then the problems get smaller faster than the number of problems increases, and the total amount of work is dominated by the work done at the very top of the recursion tree.
- If a > b^d, then the number of nodes is growing faster than the work decreases, so the amount of work is dominated by the bottom level of the tree (as in our integer multiplication example).
- If a = b^d, then the growth of the number of subproblems is exactly balanced by the decrease in the amount of work, so there is exactly the same amount of work done in total at each level of the recursion tree (namely, O(n^d)), and the overall total is hence this amount of work per level times the number of levels (as in merge sort).
Remark Master theorem generalizations [00Q6]
- October 23, 2026
The Master Theorem can be generalized in a number of ways (for example, T(n) can be less than or equal to the right-hand side instead of equal; we can handle more complex expressions for the amount of extra work besides just \Theta (n^d); n need not be evenly divisible by b; and so on). I have presented the simple core of the idea here, and if you were to ever need a generalized version for some reason, you could look it up.
Before we get to the proof of the theorem itself, we will need a few small lemmas.
Lemma Log powers [00PZ]
- October 23, 2026
For any real numbers a,b > 1 and x > 1, a^{\log _x b} = b^{\log _x a}.
Proof.
- October 23, 2026
Lemma Asymptotic behavior of geometric series [00Q0]
- October 23, 2026
For some positive real constant r and any natural number n, let S = 1 + r + r^2 + \dots + r^n.
- If 0 < r < 1, then S is \Theta (1).
- If r = 1, then S is \Theta (n).
- If r > 1, then S is \Theta (r^n).
Proof.
- October 23, 2026
- If r < 1, then the infinite sum 1 + r + r^2 + \dots converges to the constant value \frac {1}{1-r}. The partial sum up to k is less than this, so it cannot depend on n either.
- If r = 1, then S = 1 + 1 + 1 + \dots + 1 = n+1, which is \Theta (n).
- If r > 1, then S = \frac {1 - r^{n+1}}{1 - r} = r \cdot r^n + \frac {1}{r-1} which is \Theta (r^n).
Proof of the Master Theorem [00Q7]
- October 23, 2026
We now have the tools we need to prove the Master Theorem.
Proof.
- October 23, 2026
- The tree has height \log _b n.
- A node at level k has size n/b^k and does (n/b^k)^d work.
- There are a^k nodes at level k, so the total work at level k is a^k \cdot \left (\frac {n}{b^k}\right )^d = n^d \left (\frac {a}{b^d}\right )^k.
Hence the overall total work is \sum _{k=0}^{\log _b n} n^d \left (\frac {a}{b^d}\right )^k = n^d \sum _{k=0}^{\log _b n} \left (\frac {a}{b^d}\right )^k. This is n^d times a geometric series with ratio a/b^d.
- If a < b^d then the ratio is < 1 and the sum is a constant; hence the total work is \Theta (n^d).
- If a = b^d then the ratio is 1 and the sum is \Theta (\log _b n); hence the total work is \Theta (n^d \log n).
- If a > b^d then the ratio is > 1 and the sum is proportional to its final term, (a/b^d)^{\log _b n}. In that case the total work is \Theta \left ( n^d \cdot \left (\frac {a}{b^d}\right )^{\log _b n} \right ) = \Theta \left ( n^d \cdot \frac {a^{\log _b n}}{(b^{\log _b n})^d}\right ) = \Theta (a^{\log _b n}) = \Theta (n^{\log _b a}).
Algorithm Karatsuba's Algorithm [00Q8]
- October 23, 2026
Now, back to n-bit integer multiplication! In 1960 it seemed “obvious” that integer multiplication was \Omega (n^2); Andrey Kolmogorov (a very prominent mathematician at the time) posed it as a conjecture. At a special seminar organized by Kolmogorov to investigate this conjecture and other related problems, a previously unknown 23-year-old graduate student, Anatoly Karatsuba, disproved the conjecture by coming up with a faster algorithm!
Break up A and B into two pieces of size n/2 as before: A = A_1 2^{n/2} + A_2 and B = B_1 2^{n/2} + B_2. Now for Karatsuba’s clever insight. Define \begin {align*} P_1 &= A_1 B_1 \\ P_2 &= A_2 B_2 \\ P_3 &= (A_1 + A_2)(B_1 + B_2) \end {align*} Note P_3 - P_1 - P_2 = A_1B_2 + A_2 B_1. Hence AB = P_1 2^n + (P_3 - P_1 - P_2) 2^{n/2} + P_2. This requires only three multiplications of size n/2! (Along with two additions of size n/2, four additions or subtractions of size n, and two shifts—more work than before, but all still \Theta (n) in total.)
Hence T(n) = 3T(n/2) + \Theta (n), so we can apply the Master Theorem with a = 3, b = 2, and d = 1. 3 > 2^1 so we are in the third case of the theorem (the work is concentrated at the bottom of the call tree), and we conclude that this algorithm is O(n^{\log _2 3}) \approx O(n^{1.59}).
This is not even the fastest known algorithm—for example, the Toom-Cook algorithm is a generalization of Karatsuba's algorithm which splits up the numbers into more than two pieces; and the fastest algorithm actually used in practice is the Schönhage-Strassen algorithm, which is based on different ideas (the Fast Fourier Transform) can multiply two n-bit integers in O(n \log n \log \log n). In 2019 it was even proved that it is possible to multiply two n-bit integers in \Theta (n \lg n) time, although the algorithm involved would not be faster in practice for any numbers that fit in our universe!
Median/Quickselect [00Q9]
- October 26, 2026
We will consider the \mathsf {Quickselect} algorithm as one final example of a divide & conquer approach.
Motivation [00QA]
- October 26, 2026
Consider the following problem: given a (not necessarily sorted!) array of n integers, find the median element (that is, the element which would be in the middle if the array were sorted).
One obvious solution is to first sort the array in \Theta (n \log n) time, and then simply access the array in the middle. However, intuitively it feels like this is doing too much work: we don’t actually care about the order of any of the other elements, so sorting them is a waste of time. Can we do better than \Theta (n \log n)?
Attempting divide & conquer [00QB]
- October 26, 2026
We consider a divide-and-conquer approach. First, if we simply split the list in half, it doesn’t seem to help much. We could find the median of both sides but there’s no way to compute the median of the whole list from the two medians. But simply splitting the list in half is not the only way to divide up a list. Since we were already thinking about sorting, what if we partition the list so that all the smaller elements are on one side and all the larger elements are on the other side? If we pick a random element as the pivot, we can partition the list into the elements less than the pivot on the left and greater than or equal to the pivot on the right, as in quicksort, in \Theta (n) time.
Now what? Simply finding the median of both sides still does not help. We can definitely say that the true median lies somewhere in between the left and right medians but that does not really help.
Generalize the induction hypothesis [00QC]
- October 27, 2026
The solution—as often with recursive algorithms—is to generalize to compute something more than what is required. In particular, instead of just finding the median element, let’s write an algorithm to select the element which would be at index k if the list were sorted. If we can do this, we can get the median by selecting the element at index \lfloor n/2 \rfloor . But this ability to select any element gives us the extra flexibility we need to use recursion to find the median, since the median of the entire list will not be the median of one of the two partitions, but some other index.
Algorithm Quickselect [00QD]
- October 27, 2026
\mathsf {Partition}(A) picks a random pivot value in A, and returns two lists: one containing all the values in A which are less than the pivot, and one containing all values greater than or equal to the pivot.
Note that in practice, one would implement this so it all works in-place on the array A, instead of generating new arrays A_1 and A_2, e.g. by passing along extra \mathit {hi} and \mathit {lo} parameters to specify which part of the array to focus on.
Theorem \mathsf {Quickselect} is correct [00QE]
- October 27, 2026
For any array A and any 0 \leq k < |A|, \mathsf {Quickselect}(A,k) correctly returns the element with order index k, that is, the element which would be at index k in a sorted version of A.
Proof.
- October 27, 2026
By strong induction on the size of A.
- When |A|=1, since 0 \leq k < |A| = 1, we must have k = 0. The algorithm returns A[0], which is indeed the item with index 0 in a sorted version of A, since a 1-element array is already sorted.
Otherwise, let |A| = n and assume as our induction hypothesis that (A',k') works correctly on any A' which is smaller than n.
Note that after the call to , all the elements in A_1 are smaller than all the elements in A_2. Put another way, if we sorted A, all the elements in A_1 would come first, followed by all the elements in A_2. Thus, if k < |A_1|, then the element of A with order index k falls somewhere within A_1, and in fact is the element of A_1 with the same order index. On the other hand, if k \geq |A_1|, then the order-index k element of A falls somewhere inside A_2. If k = |A_1| then we want the smallest element of A_2; if k = |A_1| + 1 then we want the second-smallest, and so on; in general, the kth smallest element of A will be the k - |A_1| smallest element of A_2. Since the IH tells us that the recursive calls to \mathsf {Quickselect} will correctly select these elements from A_1 or A_2, we conclude that \mathsf {Quickselect} is correct.
Theorem \mathsf {Quickselect} runtime analysis [00QF]
- October 27, 2026
\mathsf {Quickselect} runs in expected \Theta (n) time.
Proof.
- October 27, 2026
\mathsf {Quickselect} is a randomized algorithm, since the pivot value is chosen randomly; in theory we could make very bad pivot choices that would lead to bad performance. If the pivot is chosen randomly, however, we can expect that on average it will split the array into pieces which are 1/4 and 3/4 the size of the original array, respectively (or vice versa).
We can therefore analyze \mathsf {Quickselect} using the Master Theorem. We make one recursive call each time, so a = 1; in the worst case b = 4/3; and d = 1, since we spend \Theta (n) time partitioning A. Therefore a < b^d since 1 < 4/3, so this is the first case of the Master Theorem, and we conclude that \mathsf {Quickselect} takes \Theta (n^d) = \Theta (n) time.