Itroductio to Algorithms 6.046J/8.40J/SMA5503 Lecture 4 Prof. Charles E. Leiserso
Quicsort Proposed by C.A.R. Hoare i 962. Divide-ad-coquer algorithm. Sorts i place lie isertio sort, but ot lie merge sort. Very practical with tuig. Day 6 Itroductio to Algorithms L4.2
Divide ad coquer Quicsort a -elemet array:. Divide: Partitio the array ito two subarrays aroud a pivot x such that elemets i lower subarray x elemets i upper subarray. x xx x 2. Coquer: Recursively sort the two subarrays. 3. Combie: Trivial. Key: Liear-time partitioig subroutie. Day 6 Itroductio to Algorithms L4.3
Partitioig subroutie PARTITIONA, p, q A[p.. q] x A[p] pivot A[p] i p for j p + to q do if A[ j] x the i i + exchage A[i] A[ j] exchage A[p] A[i] retur i Ivariat: Ruig time O for elemets. xx x x?? p i j q Day 6 Itroductio to Algorithms L4.4
Example of partitioig 66 0 3 55 88 33 22 i j Day 6 Itroductio to Algorithms L4.5
Example of partitioig 66 0 3 55 88 33 22 i j Day 6 Itroductio to Algorithms L4.6
Example of partitioig 66 0 3 55 88 33 22 i j Day 6 Itroductio to Algorithms L4.7
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 i j Day 6 Itroductio to Algorithms L4.8
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 i j Day 6 Itroductio to Algorithms L4.9
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 i j Day 6 Itroductio to Algorithms L4.0
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 i j Day 6 Itroductio to Algorithms L4.
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 i j Day 6 Itroductio to Algorithms L4.2
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 i j Day 6 Itroductio to Algorithms L4.3
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 i j Day 6 Itroductio to Algorithms L4.4
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 i j Day 6 Itroductio to Algorithms L4.5
Example of partitioig 66 0 3 55 88 33 22 66 55 3 0 88 33 22 66 55 33 0 88 3 22 66 55 33 22 88 3 0 22 55 33 66 88 3 0 i Day 6 Itroductio to Algorithms L4.6
Pseudocode for quicsort QUICKSORTA, p, r if p < r the q PARTITIONA, p, r QUICKSORTA, p, q QUICKSORTA, q+, r Iitial call: QUICKSORTA,, Day 6 Itroductio to Algorithms L4.7
Aalysis of quicsort Assume all iput elemets are distict. I practice, there are better partitioig algorithms for whe duplicate iput elemets may exist. Let T worst-case ruig time o a array of elemets. Day 6 Itroductio to Algorithms L4.8
Worst-case of quicsort Iput sorted or reverse sorted. Partitio aroud mi or max elemet. Oe side of partitio always has o elemets. T T 0 + T Θ + T T Θ 2 + Θ + Θ + Θ arithmetic series Day 6 Itroductio to Algorithms L4.9
Worst-case recursio tree T T0 + T + c Day 6 Itroductio to Algorithms L4.20
Worst-case recursio tree T T T0 + T + c Day 6 Itroductio to Algorithms L4.2
Worst-case recursio tree T T0 + T + c c T0 T Day 6 Itroductio to Algorithms L4.22
Worst-case recursio tree T T0 + T + c c T0 c T0 T 2 Day 6 Itroductio to Algorithms L4.23
Worst-case recursio tree T T0 + T + c c T0 c T0 c 2 T0 O Θ Day 6 Itroductio to Algorithms L4.24
Worst-case recursio tree T T0 + T + c c T0 c T0 c 2 T0 Θ O Θ Θ 2 Day 6 Itroductio to Algorithms L4.25
h Worst-case recursio tree c Θ c T T0 + T + c Θ c 2 Θ O Θ Θ Θ 2 T Θ + Θ 2 Θ 2 Day 6 Itroductio to Algorithms L4.26
Best-case aalysis For ituitio oly! If we re lucy, PARTITION splits the array evely: T 2T/2 + Θ Θ lg same as merge sort What if the split is always 9 :? 0 0 9 + T + Θ T T 0 0 What is the solutio to this recurrece? Day 6 Itroductio to Algorithms L4.27
Aalysis of almost-best case T Day 6 Itroductio to Algorithms L4.28
Aalysis of almost-best case c T 9 0 T 0 Day 6 Itroductio to Algorithms L4.29
Aalysis of almost-best case c c 0 9 c 0 9 9 8 T 00 T 00 T 00 T 00 Day 6 Itroductio to Algorithms L4.30
Aalysis of almost-best case 00 c c 0 c 9 c 0 log 0/9 9 c 9 c 8 c 00 00 00 c c c Θ O leaves Θ Day 6 Itroductio to Algorithms L4.3
Aalysis of almost-best case 00 c c 0 c 9 c 0 log 0 log 0/9 9 c 9 c 8 c 00 00 00 c c c Θ Θ lg Lucy! O leaves c log 0 Θ T c log 0/9 + Ο Day 6 Itroductio to Algorithms L4.32
More ituitio Suppose we alterate lucy, ulucy, lucy, ulucy, lucy,. L 2U/2 + Θ lucy U L + Θ ulucy Solvig: L 2L/2 + Θ/2 + Θ 2L/2 + Θ Θ lg Lucy! How ca we mae sure we are usually lucy? Day 6 Itroductio to Algorithms L4.33
Radomized quicsort IDEA: Partitio aroud a radom elemet. Ruig time is idepedet of the iput order. No assumptios eed to be made about the iput distributio. No specific iput elicits the worst-case behavior. The worst case is determied oly by the output of a radom-umber geerator. Day 6 Itroductio to Algorithms L4.34
Radomized quicsort aalysis Let T the radom variable for the ruig time of radomized quicsort o a iput of size, assumig radom umbers are idepedet. For 0,,,, defie the idicator radom variable X if PARTITION geerates a : split, 0 otherwise. E[X ] Pr{X } /, sice all splits are equally liely, assumig elemets are distict. Day 6 Itroductio to Algorithms L4.35
Aalysis cotiued T T0 + T + Θ if 0: split, T + T 2 + Θ if : 2 split, M T + T0 + Θ if : 0 split, 0 X T + T + Θ. Day 6 Itroductio to Algorithms L4.36
Calculatig expectatio E[ T ] E 0 X T + T + Θ Tae expectatios of both sides. Day 6 Itroductio to Algorithms L4.37
Day 6 Itroductio to Algorithms L4.38 Calculatig expectatio [ ] + Θ + + Θ + 0 0 ] [ T T X E T T X E E T Liearity of expectatio.
Day 6 Itroductio to Algorithms L4.39 Calculatig expectatio [ ] [ ] [ ] + Θ + + Θ + + Θ + 0 0 0 ] [ T E T X E T T X E T T X E E T Idepedece of X from other radom choices.
Day 6 Itroductio to Algorithms L4.40 Calculatig expectatio [ ] [ ] [ ] [ ] [ ] Θ + + + Θ + + Θ + + Θ + 0 0 0 0 0 0 ] [ E T E T T E T X E T T X E T T X E E T Liearity of expectatio; E[X ] /.
Day 6 Itroductio to Algorithms L4.4 Calculatig expectatio [ ] [ ] [ ] [ ] [ ] [ ] 2 ] [ 0 0 0 0 0 0 E T E T E T T E T X E T T X E T T X E E T + Θ Θ + + + Θ + + Θ + + Θ + Summatios have idetical terms.
Hairy recurrece [ ] 2 E T 2 [ ] + Θ E T The 0, terms ca be absorbed i the Θ. Prove: E[T] alg for costat a > 0. Choose a large eough so that alg domiates E[T] for sufficietly small 2. Use fact: lg 2 lg 2 2 2 Day 6 Itroductio to Algorithms L4.42 8 exercise.
Substitutio method 2 [ ] a lg + Θ E T 2 Substitute iductive hypothesis. Day 6 Itroductio to Algorithms L4.43
Substitutio method [ ] E T 2 Use fact. 2a 2 2 a lg 2 lg + Θ 8 2 + Θ Day 6 Itroductio to Algorithms L4.44
Substitutio method [ ] E T 2 2a 2 2 a lg lg + Θ a lg a Θ 4 Express as desired residual. 2 8 2 + Θ Day 6 Itroductio to Algorithms L4.45
Substitutio method [ ] E T 2 2a 2 a lg a lg 2 a, a 4 + Θ + Θ Day 6 Itroductio to Algorithms L4.46 lg 2 lg 8 2 Θ if a is chose large eough so that a/4 domiates the Θ.
Quicsort i practice Quicsort is a great geeral-purpose sortig algorithm. Quicsort is typically over twice as fast as merge sort. Quicsort ca beefit substatially from code tuig. Quicsort behaves well eve with cachig ad virtual memory. Day 6 Itroductio to Algorithms L4.47