Professional Documents
Culture Documents
08 - Sorting in Linear Time
08 - Sorting in Linear Time
Hsu, Lih-Hsing
Computer Theory Lab.
a2:a3 a1:a3
Chapter 8 P.2
Computer Theory Lab.
Chapter 8 P.3
Computer Theory Lab.
Chapter 8 P.4
Computer Theory Lab.
A n a l y s i s : O ( k n )
S p e c i a l c a s e : O ( n ) w h e n k O ( n ) .
P r o p e r t y : s t a b l e ( n u m b e r w i t h t h e s a m e v a l u e a p p e a r i n t h e
o u tp u t a r r a y in th e s a m e o r d e r a s th e y d o in th e in p u t a r r a y .)
Chapter 8 P.5
Computer Theory Lab.
Chapter 8 P.6
Computer Theory Lab.
RADIX_SORT(A,d)
1 for i 1 to d
2 do use a stable sort to sort array A on digit i
Chapter 8 P.7
Computer Theory Lab.
Analysis: O( d ( n k )) O( dn dk )
NOTE: not in-place (in-place: only constant number of elements
of the input array are ever stored outside the array.)
Lemma 8.3
Given n d-digit numbers in which each digit can take on up to k
possible values, RADIX-SORT correctly sorts these number in
(d(n + k)) time.
Chapter 8 P.8
Computer Theory Lab.
Lemma 8.4
Given n b-bit numbers and any positive integer r ≤ b, RADIX- SORT
correctly sorts these numbers in ((b/r)(n+2r)) time.
Chapter 8 P.9
Computer Theory Lab.
Chapter 8 P.10
Computer Theory Lab.
Analysis
The running time of bucket sort is
n 1
T (n) (n) O (ni2 ).
i 0
taking expectations of both sides and using linearity of
expectation, we have
n1
E[T (n)] E (n) O(ni2 )
i 0
n 1
(n) E[O(ni2 )]
i 0
n 1
(n) O[ E (ni2 )]
i 0
Chapter 8 P.11
Computer Theory Lab.
We claim that
E[ni2 ] 2 1 / n
Chapter 8 P.12
Computer Theory Lab.
n 2
E[ni2 ] E X ij
j 1
n n
E X ij X ik
j 1k 1
n
E X ij2 X ij X ik
j 1 1 j n 1 k n
k j
n
E[ X ij2 ] E[ X ij X ik ],
j 1 1 j n 1k n
k j
Chapter 8 P.13
Computer Theory Lab.
n 1 1
E[ni2 ] n 2
j 1 1 j n 1 k n n
k j
1 1
n n(n 1) 2
n n
n 1
1
n
1
2
n
We can conclude that the expected time for bucket sort is
(n)+n·O(2-1/n)= (n).
Chapter 8 P.15
Computer Theory Lab.
Another analysis:
n 1 2 n 1 2 n 1
O( E [ ni ]) O( E [ ni ]) O( (1 )) O( n )
i 0 i 0 i 0
Because
E( ni2 ) Var [ ni ] E 2 [ ni ]
1
E [ ni ] np 1 where p=
n
1
Var [ ni ] np(1 p ) 1
n
1 2 1
E( ni2 ) 1 1 2 (1 )
n n