randomizing quick sort
TRANSCRIPT
-
7/24/2019 Randomizing Quick Sort
1/46
Analysis of AlgorithmsCS 477/677
Randomizing Quicksort
Instructor: Gorg !"is
(Appendix C.2 , Appendix C.3)
(Chapter 5, Chapter 7)
-
7/24/2019 Randomizing Quick Sort
2/46
#
Randomizing Quicksort
$ Randomly %rmut th lmnts of th in%utarray "for sorting
$ &R ''' modify th (AR)I)I&* %rocdur
+At ach st% of th algorithm , -chang lmnt
A[p],ith an lmnt chosn at random from A[pr]
+ )h %i.ot lmnt x = A[p]is ually likly to " anyon of th r p + 1lmnts of th su"array
-
7/24/2019 Randomizing Quick Sort
3/46
0
Randomizd Algorithms
$ *o in%ut can licit ,orst cas "ha.ior+ 1orst cas occurs only if , gt 2unlucky3 num"rs
from th random num"r gnrator
$ 1orst cas "coms lss likly+ Randomization can *&) liminat th ,orstcas "ut
it can mak it lss likly5
-
7/24/2019 Randomizing Quick Sort
4/46
4
Randomizd (AR)I)I&*
Alg.:RA*&I89(AR)I)I&*(A, p, r)
i RA*&(p, r)
-chang A[p] A[i]
return (AR)I)I&*(A, p, r)
-
7/24/2019 Randomizing Quick Sort
5/46
Randomizd Quicksort
Alg. :RA*&I89Q;IC
-
7/24/2019 Randomizing Quick Sort
6/46
6
=ormal 1orstCasAnalysis of Quicksort
$)>n? @ ,orstcas running tim)>n? @ ma- >)>? )>n?? >n?
B C / C n4B
$ ;s su"stitution mthod to sho, that th runningtim of Quicksort is &>n#?
$ Guss )>n? @ &>n#?
+ Induction goal: )>n? cn#
+ Induction hy%othsis: )>k? ck#for any kD n
-
7/24/2019 Randomizing Quick Sort
7/467
1orstCas Analysis of Quicksort
$(roof of induction goal:)>n? ma- >c# c>n?#? >n? @
B nB
@ c ma- ># >n?#? >n?
B nB$ )h -%rssion # >n?#achi.s a ma-imum o.r th
rang B nB at on of th nd%oints
ma- ># >n ?#? @ B# >n B?#@ n#+ #>n + B?B nB
)>n? cn#+ #c>n + B? >n?
cn#
-
7/24/2019 Randomizing Quick Sort
8/46E
R.isit (artitioning
$ Foars %artition+ Slct a %i.ot lmnt xaround ,hich to %artition
+ Gro,s t,o rgions
A[pi] xx A[jr]
A[pi] x x A[jr]
i H
-
7/24/2019 Randomizing Quick Sort
9/46
Anothr 1ay to (AR)I)I&*>Jomutos %artition + %ag B46?
$ Gi.n an array AK %artition th
array into th follo,ing su"arrays:
+ A %i.ot lmnt x = A[q]
+Su"array A[p..q-1] such that ach lmnt of A[p..q-1]is smallrthan or ual to x>th %i.ot?
+ Su"array A[q+1..r]K such that ach lmnt of A[p..q+1]is strictly
gratr than x>th %i.ot?
$ )h %i.ot lmnt is not includd in any of th t,o
su"arrays
A[pi] x A[i+1j-1] > x
% i iB rHB
unkno,n
%i.ot
H
-
7/24/2019 Randomizing Quick Sort
10/46
BL
9-am%l
at th ndK
s,a% %i.ot
-
7/24/2019 Randomizing Quick Sort
11/46
BB
Anothr 1ay to (AR)I)I&* >contd?
Alg': (AR)I)I&*(A, p, r)
x A[r]
i p - 1
forj p to r - 1
do if A[ j ] x then i i + 1
-chang A[i]M A[j]
-chang A[i + 1]M A[r]
return i + 1
Chooss th last lmnt of th array as a %i.otGro,s a su"array N%''iO of lmnts -Gro,s a su"array NiB''HBO of lmnts P-Running )im: >n?K ,hr n@r%B
A[pi] x A[i+1j-1] > x
% i iB rHB
unkno,n
%i.ot
H
-
7/24/2019 Randomizing Quick Sort
12/46
B#
Randomizd Quicksort>using Jomutos %artition?
Alg. :RA*&I89Q;IC
-
7/24/2019 Randomizing Quick Sort
13/46
B0
Analysis of Randomizd Quicksort
Alg. :RA*&I89Q;IC
-
7/24/2019 Randomizing Quick Sort
14/46
B4
(AR)I)I&*
Alg': (AR)I)I&*(A, p, r)
x A[r]
i p - 1
forj p to r - 1
do if A[ j ] x then i i + 1
-chang A[i]M A[j]
-chang A[i + 1]M A[r]
return i + 1
&>B? constant
&>B? constant
of com%arisons: k"t,n th %i.ot andth othr lmnts
Amount of ,ork at call k: c k
-
7/24/2019 Randomizing Quick Sort
15/46
B
A.ragCas Analysis of Quicksort
$ Jt @ total number of comparisonsperformedin all calls to PAR!!"#$
$ )h total ,ork don o.r th entire-cution ofQuicksort is
&>nc?@&>n?
$ *d to stimat 9>?
k
k
X X=
-
7/24/2019 Randomizing Quick Sort
16/46
B6
R.i, of (ro"a"ilitis
-
7/24/2019 Randomizing Quick Sort
17/46
B7
R.i, of (ro"a"ilitis
>discrt cas?
-
7/24/2019 Randomizing Quick Sort
18/46
BE
Random aria"ls
Def.:(Discrete) random variable X: afunction from a sample space S to the real
numbers.
+ It associats a ral num"r ,ith ach %ossi"l outcomof an -%rimnt'
>H?
-
7/24/2019 Randomizing Quick Sort
19/46
B
Random aria"ls
9'g': )oss a coin thr tims
dfin @ 2num"rs of hads3
C ti ( " "iliti
-
7/24/2019 Randomizing Quick Sort
20/46
#L
Com%uting (ro"a"ilitis;sing Random aria"ls
-
7/24/2019 Randomizing Quick Sort
21/46
#B
9-%ctation
$ 9-%ctd .alu >-%ctationK man? of a discrtrandom .aria"l is:
2[3] = 4xx r53 = x6
+ 2A.rag3 o.r all %ossi"l .alus of random .aria"l
-
7/24/2019 Randomizing Quick Sort
22/46
##
9-am%ls
9-am%l: @ fac of on fair dic
9NO @ BB/6 #B/6 0B/6 4B/6 B/6 6B/6 @ 0'
9-am%l:
-
7/24/2019 Randomizing Quick Sort
23/46
#0
Indicator Random aria"ls
$ Gi.n a sam%l s%ac S and an .ntAK , dfin th indicator
random variableITAU associatd ,ith A:
+ ITAU @ B if A occurs
L if A dos not occur
$ )h -%ctd .alu of an indicator random .aria"l 3A=/5A6 is:
E[XA] = Pr {A}
$ (roof:
9NAO @ 9NITAUO @ B (rTAU L (rTVU @ (rTAU
-
7/24/2019 Randomizing Quick Sort
24/46
#4
A.ragCas Analysis of Quicksort
$ Jt @ total number of comparisonsperformedin all calls to PAR!!"#$
$ )h total ,ork don o.r th entire-cution of
Quicksort is
&>n?
$ *d to stimat 9>?
k
k
X X=
-
7/24/2019 Randomizing Quick Sort
25/46
#
*otation
$ Rnam th lmnts of A as zBK z#K ' ' ' K znK ,ith
zi"ing th ith smallst lmnt
$ fin th st 8iH@ TziK ziABK ' ' ' K zHU th st of
lmnts "t,n ziand zHK inclusi.
BL6B40E 7#
zBz# z zE zz0 z4 z6 zBL z7
) t l * " f C i
-
7/24/2019 Randomizing Quick Sort
26/46
#6
)otal *um"r of Com%arisonsin (AR)I)I&*
iB n
=Xi nB
=
1
1
n
i
+=
n
ij 1 ijX
$ )otal num"r of com%arisons %rformd "yth algorithm:
$ fin iH@ I Tziis com%ard to zH U
9 t d * " f ) t l
-
7/24/2019 Randomizing Quick Sort
27/46
#7
9-%ctd *um"r of )otalCom%arisons in (AR)I)I&*
$ Com%ut th expected %alue of &$
=][XE
"y linarityof -%ctation
th -%ctation of iHis ual
to th %ro"a"ility of th .nt2ziis com%ard to zH3
=
= +=
1
1 1
n
i
n
ij
ijXE [ ]=
= +=
1
1 1
n
i
n
ij
ijXE
= +=
=1
1 1
}Pr{n
i
n
ij
ji ztocomparedisz
indicatorrandom .aria"l
C i i (AR)I)I&*
-
7/24/2019 Randomizing Quick Sort
28/46
#E
Com%arisons in (AR)I)I&* :&"sr.ation B
$ 9ach %air of lmnts is com%ard at most onceduring th ntir -cution of th algorithm
+ 9lmnts ar com%ard only to th %i.ot %oint5
+ (i.ot %oint is -cludd from futur calls to (AR)I)I&*
Com%arisons in (AR)I)I&*:
-
7/24/2019 Randomizing Quick Sort
29/46
#
Com%arisons in (AR)I)I&*:&"sr.ation #
$ &nly th %i.ot is com%ard ,ith lmnts in "oth%artitions5
zB
z#
z
zE
z
z0
z4
z6
zBL
z7
BL6B40E 7#
8BK6@ TBK #K 0K 4K K 6U 8EK@ TEK K BLUT7U%i.ot
9lmnts "t,n diffrnt %artitionsar n.r com%ard5
-
7/24/2019 Randomizing Quick Sort
30/46
0L
Com%arisons in (AR)I)I&*
$ Cas B: %i.ot chosn such as: 8i < x < 8j
8iand 8j,ill n.r " com%ard
$ Cas #9 8i or8jis th %i.ot 8i and8j,ill " com%ard
+ only if on of thm is chosn as %i.ot "for any othrlmnt in rang 8
ito8
j
'6B40E 7#
zBz# z zE zz0 z4 z6 zBL z7
8BK6@ TBK #K 0K 4K K 6U 8EK@ TEK K BLUT7U
Pr{ }?i jz is compared to z
-
7/24/2019 Randomizing Quick Sort
31/46
0B
S ,hy
z#z4zB z0zz7zz6
z# ,ill n.r "com%ard ,ith z6sinc z >,hich"longs to Nz#K z6O?
,as chosn as a
%i.ot first 5
-
7/24/2019 Randomizing Quick Sort
32/46
0#
(ro"a"ility of com%aring zi,ith zH
@ B/> H i B? B/> H i B? @ #/> H i B?
ziis com%ard to zH
ziis th first %i.ot chosn from 8iH
@(rT U
(rT U
zH is th first %i.ot chosn from 8 iH(rT U
&R
$)hr arj i + 1lmnts "t,n 8i
and 8j
+(i.ot is chosn randomly and ind%ndntly
+)h %ro"a"ility that any %articular lmnt is th firston chosn is 1:( j - i + 1)
-
7/24/2019 Randomizing Quick Sort
33/46
00
*um"r of Com%arisons in (AR)I)I&*
1 1 1 1
1 1 1 1 1 1 1
2 2 2[ ] (lg )1 1
n n n n i n n n
i j i i k i k i
E X O nj i k k
= = + = = = = =
= = < = + +
)lg( nnO=9-%ctd running tim of Quicksort usingRA*&I89(AR)I)I&* is 0(n!n)
= +=
=1
1 1
}Pr{][n
i
n
ij
ji ztocomparediszXE
9-%ctd num"r of com%arisons in (AR)I)I&*:
>st k@Hi? >harmonic sris?
Altrnati. A.rag Cas
-
7/24/2019 Randomizing Quick Sort
34/46
04
Altrnati. A.ragCasAnalysis of Quicksort
$ S (ro"lm 7#K %ag B6
$ =ocus on th -%ctd running tim ofach indi.idual rcursi. call to QuicksortK
rathr than on th num"r of com%arisons%rformd'
$;s Foar %artition in our analysis'
Altrnati. A.rag Cas
-
7/24/2019 Randomizing Quick Sort
35/46
0
Altrnati. A.ragCasAnalysis of Quicksort
>i''K any lmnt has th sam %ro"a"ility to " chosn as %i.ot?
Altrnati. A.rag Cas
-
7/24/2019 Randomizing Quick Sort
36/46
06
Altrnati. A.ragCasAnalysis of Quicksort
Altrnati. A.rag Cas
-
7/24/2019 Randomizing Quick Sort
37/46
07
Altrnati. A.ragCasAnalysis of Quicksort
Altrnati. A.rag Cas
-
7/24/2019 Randomizing Quick Sort
38/46
0E
Altrnati. A.ragCasAnalysis of Quicksort
Altrnati. A.rag Cas
-
7/24/2019 Randomizing Quick Sort
39/46
0
Altrnati. A.ragCasAnalysis of Quicksort
B:nB s%lits ha. #/n %ro"a"ility all othr s%lits ha. B/n %ro"a"ility
Altrnati. A.rag Cas
-
7/24/2019 Randomizing Quick Sort
40/46
4L
Altrnati. A.ragCasAnalysis of Quicksort
Altrnati. A.ragCas
-
7/24/2019 Randomizing Quick Sort
41/46
4B
Altrnati. A.ragCasAnalysis of Quicksort
rcurrnc for a.rag cas: Q>n? @
-
7/24/2019 Randomizing Quick Sort
42/46
4#
(ro"lm
$Considr th %ro"lm of dtrmining ,hthr anar"itrary sunc T-BK -#K '''K -nU of nnum"rscontains r%atd occurrncs of som num"r'Sho, that this can " don in W>nlgn? tim'
+ Sort th num"rs$ W>nlgn?
+ Scan th sortd sunc from lft to rightK chcking,hthr t,o succssi. lmnts ar th sam
$ W>n?+ )otal
$ W>nlgn?W>n?@W>nlgn?
-
7/24/2019 Randomizing Quick Sort
43/46
40
9- #'06 >%ag 07?
$ Can , us !inary Sarch to im%ro. InsrtionSort >i''Kfind th corrct location to insrt ANHOX?
Alg.:I*S9R)I&*S&R)(A)
forj ; to ndo e&Y A[ j ]
Insrt A[ j ]into th sortd sunc A[1 . . j -1]
i j - 1
hile i > and A[i] > e&do A[i + 1] A[i] i i 1
A[i + 1] e&
-
7/24/2019 Randomizing Quick Sort
44/46
44
9- #'06 >%ag 07?
$ Can , us "inary sarch to im%ro. InsrtionSort >i''Kfind th corrct location to insrt ANHOX?
+ )his ida can rduc th num"r of com%arisonsfrom &>n? to &>lgn?
+ *um"r of shifts stays th samK i''K &>n?
+ &.rallK tim stays th sam '''
+ 1orth,hil ida ,hn com%arisons ar -%nsi.
>'g'K com%ar strings?
-
7/24/2019 Randomizing Quick Sort
45/46
4
(ro"lm
$Analyz th com%l-ity of th follo,ing function:
*(i)
if i+
then return '
return (2*(i-'))
Rcurrnc: )>n?@)>nB?c
;s itration to sol. it '''' )>n?@W>n?
-
7/24/2019 Randomizing Quick Sort
46/46
(ro"lm
$ 1hat is th running tim of Quicksort ,hn allth lmnts ar th samX
+ ;sing Foar %artition"st cas
$ S%lit in half .ry tim$ )>n?@#)>n/#?n)>n?@W>nlgn?
+ ;sing Jomutos %artition ,orst cas
$ B:nB s%lits .ry tim$ )>n?@W>n#?