7.quicksort hsu, lih-hsing. computer theory lab. chapter 7p.2 7.1 description of quicksort divide...
Post on 18-Dec-2015
219 Views
Preview:
TRANSCRIPT
7.Quicksort
Hsu, Lih-Hsing
Chapter 7 P.2
Computer Theory Lab.
7.1 Description of quicksort Divide
Conquer
Combine
Chapter 7 P.3
Computer Theory Lab.
Q U I C K S O R T ( A , p , r )
1 i f p r
2 t h e n q P A R T I T I O N A p r ( , , )
3 Q U I C K S O R T ( A , p , q )
4 Q U I C K S O R T ( A , q + 1 , r )
Chapter 7 P.4
Computer Theory Lab.
Partition(A, p, r)
1 x A[r]
2 i p – 1
3 for j p to r -1
4 do if A[j] ≤ x
5 then i i + 1
6 exchange A[i] A[j]
7 exchange A[i +1] A[r]
8 return i +1
Chapter 7 P.5
Computer Theory Lab.
At the beginning of each iteration of the loop of lines 3-6, for any array index k,
1. if p ≤ k ≤ i, then A[k] ≤ x.
2. if i + 1 ≤ k≤ j -1, then A[k] > x.
3. if k = r, then A[k] = x.
Chapter 7 P.6
Computer Theory Lab.
The operation of Partition on a sample array
Chapter 7 P.7
Computer Theory Lab.
Two cases for one iteration of procedure Partition
Complexity: Partition on A[p…r] is (n) where n = r –p +1
Chapter 7 P.8
Computer Theory Lab.
7.2 Performance of quicksort W o r s t - c a s e p a r t i t i o n :
T n T n n
k k nk
n
k
n
( ) ( ) ( )
( ) ( ) ( )
1
1 1
2
B e s t - c a s e p a r t i t i o n :
T n T n n
T n n n
( ) ( / ) ( )
( ) ( l o g )
2 2
Chapter 7 P.9
Computer Theory Lab.
Balanced partition T n n n( ) ( log )
)log()(
)()10/()10/9()(
nnnT
nnTnTnT
Chapter 7 P.10
Computer Theory Lab.
Intuition for the average case T(n) = (n logn)
Chapter 7 P.11
Computer Theory Lab.
7.3 Randomized versions of partition
R A N D O M I Z E D _ P A R T I T I O N ( A , p , r )
1 i R A N D O M p r ( , )
2 e x c h a n g e A p A i[ ] [ ]
3 r e t u r n P A R T I T I O N ( A , p , r )
R A N D O M I Z E D _ Q U I C K S O R T ( A , p , r )
1 i f p r
2 t h e n
q R A N D O M I Z E D P A R T I T I O N A p r _ ( , , )
3 R A N D O M I Z E D _ Q U I C K S O R T ( A , p , q )
4 R A N D O M I Z E D _ Q U I C K S O R T ( A , q + 1 , r )
Chapter 7 P.12
Computer Theory Lab.
7.4 Analysis of quicksort7.4.1 Worst-case analysis
)())1()(()( max10
nqnTqTnTnq
guess T n cn( ) 2
2
2
22
10
22
10
)()1(2
)())1((
)())1(()(
max
max
cn
nnccn
nqnqc
nqnccqnT
nq
nq
pick the constant c large enough so that the 2 1c n( ) term dominates the
( )n term.
T n n( ) ( ) 2
Chapter 7 P.13
Computer Theory Lab.
7.4-2 Show that 22 )( qnq achieves a maximum over
1,.....,2,1 nq when 1q or 1nq
ans: 先令 22 )()( qnqqf
一次微分: nqqnqqf 24)(22)('
令 0)(' qf 024 nq 2
nq(極小值)
二次微分: 4)('' qf (開口向上)
因為 11 nq 所以 2)1(1)1()1( nnff (相對極大值)
nn/20
Chapter 7 P.14
Computer Theory Lab.
7.4.2 Expected running time Running time and comparsions Lemma 7.1
Let X be the number of comparisons performed in line 4 of partition over the entire execution of Quicksort on an n-element array. Then the running rime of Quicksort is O(n+X)
Chapter 7 P.15
Computer Theory Lab.
we define
{zi is compared to zj},
{zi is compared to zj}
IX ij
.1
1 1
n
i
n
ijijXX
1
1 1
1
1 1
1
1 1
Pr
][
n
i
n
ij
n
i
n
ijij
n
i
n
ijij
XE
XEXE
Chapter 7 P.16
Computer Theory Lab.
Pr{zi is compared to zj} = Pr{zi or zj is first pivot chosen from Zij}
= Pr{zi is first pivot chosen from Zij}
+ Pr{zj is first pivot chosen from Zij}
1
1
1
1
ijij
1
2
ij
1
1 1
.1
2][
n
i
n
ij ijXE
Chapter 7 P.17
Computer Theory Lab.
)lg(
)(lg
2
1
2
1
2][
1
1
1
1 1
1
1 1
1
1 1
nnO
nO
k
k
ijXE
n
i
n
i
n
k
n
i
in
k
n
i
n
ij
Chapter 7 P.18
Computer Theory Lab.
another analysis
T n
nT T n T q T n q n
q
n
( )
( ( ) ( ) ( ( ) ( ) ) ( )
11 1
1
1
T
T n O n nT T n O n
( )
( ) ( )( ( ) ( ) ) ( )
1 1
1
11 1
2
T n
nT T n T q T n q n
nT q T n q n
nT k n
q
n
q
n
k
n
( )
( ( ) ( ) ( ( ) ( ) ) ( )
( ( ) ( ) ) ( )
( ( ) ) ( )
11 1
1
2
1
1
1
1
1
1
Chapter 7 P.19
Computer Theory Lab.
g u e s s bnannT log)(
T nn
a k k b n
an
k kb
nn n
k
n
k
n
( ) ( l o g ) ( )
l o g ( ) ( )
2
2 21
1
1
1
1
W e w i l l p r o v e k kn n n
k
nl o g
l o g
2
1
1 2
2 8
Chapter 7 P.20
Computer Theory Lab.
bnan
anbnbnan
nban
nan
nn
nbnnn
na
nT
log
)4
)((log
)(24
log
)()1(2
)8
log21
(2
)(2
2
C h o o s e a l a r g e e n o u g h s o t h a t a n
n b4
( ) .
T n O n n( ) ( l o g ) .
Chapter 7 P.21
Computer Theory Lab.
.2 if82
log2
)12
(2
1
2
log)1(
log
log)1(log
logloglog
22
12/
1
1
1
1
2/
12/
1
1
2/
1
1
12/
1
n
nnn
nnnnn
kkn
knkn
kkkkkk
n
k
n
k
n
nk
n
k
n
nk
n
k
n
k
A n o t h e r a p p r o a c h : U s i n g
x x d x x x xl n l n 12
14
2 2
top related