Divide and conquer
הוצג
5:37
עיצוב וניתוח אלגוריתמים
This course teaches you to price an algorithm's design before you trust it with real data. The first two units cover analysis tools and divide and conquer. The…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| Divide and conquer/dɪˈvaɪd ænd ˈkɒŋkə/ | solving a problem by splitting it into smaller problems of the same kind and combining their answers |
| Merge sort/mɜːdʒ sɔːt/ | a sort that splits the list in half, sorts each half and merges the two sorted halves |
| Quicksort/ˈkwɪksɔːt/ | a sort that partitions the list around a pivot and then sorts each side the same way |
| pivot/ˈpɪvət/ | the element a partition step compares every other element with |
| Karatsuba/ˌkærətˈsjuːbə/ | a divide-and-conquer multiplication method using three half-size multiplications instead of four |
| inversion/ɪnˈvɜːʃn/ | A pair of sequence positions whose values occur in the opposite order to the desired sorted order. |
Split, solve, combine
- Divide and conquer 分治 splits a problem, solves the parts and combines the answers. Merge sort 归并排序 splits in half, sorts each half and merges in $\Theta(n)$: $T(n) = 2T(n/2) + \Theta(n) = \Theta(n\log n)$.
- Quicksort 快速排序 moves the work into the partition step, so the choice of pivot 主元 decides everything.

Binary search recurrence is —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
One recursive call, constant combine: Theta(log n).
Quicksort's three cases
- Balanced pivots: $\Theta(n\log n)$. Sorted input with the first element as pivot: $T(n) = T(n-1) + \Theta(n) = \Theta(n^2)$.
- Course measurements on the 64-element array: merge sort 归并排序 310 comparisons, quicksort 356, and sorted input in the worst case $64 \times 63/2 = 2016$. With random pivots the expected count is $2n\ln n \approx 1.39\,n\log_2 n$.
Sorted input with first-element pivots costs quicksort —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
T(n) = T(n-1) + n: each partition peels one element.
Quicksort that always picks the first element as pivot runs in $\Theta(n \log n)$ on input that is already sorted.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
On sorted input the first element is the smallest, so every partition splits $0$ וגם $n - 1$: $T(n) = T(n-1) + \Theta(n) = \Theta(n^2)$, $2016$ comparisons on the 64-element array. Random pivots reduce the likelihood of this worst case.
Two worked patterns
- Maximum subarray by divide and conquer 分治: the best is in the left half, in the right half, or crossing the middle. The crossing scan is a full pass, so the total is $\Theta(n\log n)$. On $[-2, 1, -3, 4, -1, 2, 1, -5, 4]$ the answer is 6.
- Binary search is the simplest case: one recursive call and a constant combine step, giving $\Theta(\log n)$. The pattern still works with only one branch.
The maximum subarray of [-2, 1, -3, 4, -1, 2, 1, -5, 4] sums to —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
The run 4, -1, 2, 1 sums to 6.
Karatsuba: three products, not four
- Multiplying two $n$-digit numbers by halves needs four half-size products: $T(n) = 4T(n/2) + n = \Theta(n^2)$. Karatsuba 卡拉楚巴算法 needs only three: $ac$, $bd$ and $(a+b)(c+d)$, because the middle term is $(a+b)(c+d) - ac - bd$.
- For $12 \times 34$, split into $a = 1$, $b = 2$ and $c = 3$, $d = 4$:
| Product | Value |
|---|---|
| $ac = 1 \times 3$ | 3 |
| $bd = 2 \times 4$ | 8 |
| $(a+b)(c+d) = 3 \times 7$ | 21 |
| middle $= 21 - 3 - 8$ | 10 |
- Put the pieces back in their places, three multiplications instead of four:
- Now $T(n) = 3T(n/2) + n = \Theta(n^{\log_2 3}) \approx \Theta(n^{1.585})$. At $1024$ digits that is $3^{10} = 59\,049$ digit products against $1\,048\,576$. Strassen uses the same trick for matrices: $7$ products instead of $8$.
Karatsuba on 23 x 45 splits into a = 2, b = 3, c = 4, d = 5. What is the middle term (a+b)(c+d) - ac - bd?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
(5)(9) = 45, minus 8 and 15, gives 22; the product is 800 + 220 + 15 = 1035.
Fully recursive Karatsuba multiplies two 256-digit numbers. How many single-digit products does it make?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
256 = 2^8, so 8 levels of 3 products: 3^8 = 6561.
Closest pair in n log n
- Checking every pair of $n$ points costs $\Theta(n^2)$. Divide and conquer 分治 sorts the points by $x$, splits at the median and solves each half, which gives a best distance $\delta$.
- A closer pair must cross the line inside a strip of width $2\delta$. With the strip sorted by $y$, each point needs comparing with at most 7 following points, so the combine step is $\Theta(n)$ and the total is $\Theta(n\log n)$.
In the closest-pair strip, at most how many following points must each point be compared with?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Only 8 points fit in a $\delta \times 2\delta$ box with all pairs at least $\delta$ apart.
Worked example: counting inversions while merging
- An inversion 逆序 is a pair in the wrong order. $[2, 4, 1, 3, 5]$ has three: (2,1), (4,1), (4,3). Checking all pairs is $\Theta(n^2)$.
- Merge sort counts them at no extra cost. When an element from the right half is taken before $k$ remaining left elements, it closes $k$ inversions. Merging $[2, 4]$ with $[1, 3, 5]$: taking 1 adds 2 and taking 3 adds 1, a total of $\mathbf{3}$ in $\Theta(n\log n)$.
How many inversions does $[5, 4, 3, 2, 1]$ have?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Every pair is reversed: $\binom{5}{2} = 10$.
How many inversions does $[3, 1, 2]$ have?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
(3,1) and (3,2).
Marks that slip away
- Ignoring the combine cost. Merge sort's $\Theta(n)$ merge is what makes it $n\log n$; a $\Theta(n^2)$ combine would ruin it.
- Assuming an even split. Quicksort's split depends on the pivot 主元; a split of $0$ and $n - 1$ gives $\Theta(n^2)$.
- Expecting Karatsuba to win at every size. Three half-size products give about $n^{1.585}$, but the extra additions make it slower than the school method for small $n$.
Recap
- Divide, solve, combine: merge sort 归并排序 is $2T(n/2) + n = \Theta(n \log n)$.
- Quicksort's pivot decides: $\Theta(n \log n)$ when balanced, $\Theta(n^2)$ on sorted input with the first element as pivot.
- Fewer subproblems change the exponent: Karatsuba's $3$ gives $n^{1.585}$.
מה השתבש? רק צוות האתר רואה זאת.