Divide and conquer
Introducido
5:37
Diseño y análisis de algoritmos
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…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| 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. |
Divide, resuelve, combina
- Ordenación por fusión divide a la mitad, recursiona y fusiona en $\Theta(n)$: $T(n) = 2T(n/2) + \Theta(n) = \Theta(n\log n)$. Quicksort traslada el trabajo al particionado — la calidad del pivote lo decide todo.


Binary search recurrence is —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
One recursive call, constant combine: Theta(log n).
Tres mundos del Quicksort
- Pivotes equilibrados: $\Theta(n\log n)$. Entrada ordenada con pivotes del primer elemento: $T(n) = T(n-1) + \Theta(n) = \Theta(n^2)$.
- Mediciones del curso en el array de elementos 64: fusión 310 comparaciones, quicksort 356, peor caso entrada ordenada 2080. Los pivotes aleatorios hacen que el conteo esperado sea $2n\ln n \approx 1.39\,n\log_2 n$.
Sorted input with first-element pivots costs quicksort —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
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.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
On sorted input the first element is the smallest, so every partition splits $0$ y $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.
Dos patrones trabajados
- Subarray máximo por divide y vencerás: mejor izquierda, mejor derecha o la mejor que cruza el medio — el recorrido de cruce es un paso completo, así que $\Theta(n\log n)$. En $[-2, 1, -3, 4, -1, 2, 1, -5, 4]$ la respuesta es 6.
- La búsqueda binaria es el caso degenerado: una llamada recursiva, combinación constante, $\Theta(\log n)$ — el patrón sobrevive con una sola rama.
The maximum subarray of [-2, 1, -3, 4, -1, 2, 1, -5, 4] sums to —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
The run 4, -1, 2, 1 sums to 6.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
(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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
256 = 2^8, so 8 levels of 3 products: 3^8 = 6561.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Only 8 points fit in a $\delta \times 2\delta$ box with all pairs at least $\delta$ apart.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Every pair is reversed: $\binom{5}{2} = 10$.
How many inversions does $[3, 1, 2]$ have?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
(3,1) and (3,2).
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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$.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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}$.
¿Qué salió mal? Solo el equipo del sitio ve esta información.