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}$.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้