Download Chapter8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
8.Sorting in linear time
Computer Theory Lab.
8.1 Lower bound for sorting
The decision tree model
a1:a2


a2:a3


<1,2,3>

a1:a3

<1,3,2>
Chapter 8
a1:a3

<2,1,3>

<3,1,2>
a2:a3

<2,3,1>

<3.2,1>
P.2
Computer Theory Lab.
Theorem 9.1. Any decision tree that sorts n elements has
height ( n log n ) .
Proof:
n! l  2h ,
h  log( n!)  (n lg n).
Corollary 9.2 Heapsort and merge sort are asymptotically
optimal comparisons.
Chapter 8
P.3
Computer Theory Lab.
8.2 Counting sort

Assume that each of the n input elements is an
integer in the range 1 to k for some integer k.
COUNTING_SORT(A,B,k)
1 for i 1 to k
2
do c [ i ]  0
3 for j 1 to length[A]
4
6 for i  2 to k
7
8 ► c[i] now contains the number of
do c [ A[ j ]]  c [ A[ j ]] 1
5 ► c[i] now contains the number
of elements equal to i
elements less than or equal to i
9 for j  length [ A ] downto 1
10
11
Chapter 8
do c [ i ]  c [ i ]  c [ i 1 ]
do B[ c [ A[ j ]]]  A[ j ]
c [ A[ j ]]  c [ A[ j ]] 1
P.4
Computer Theory Lab.

Analysis: O( k  n )

Special case: O( n ) when k  O( n ) .

Property: stable (number with the same value appear in the
output array in the same order as they do in the input array.)
Chapter 8
P.5
Computer Theory Lab.
The operation of Counting-sort on an input array A[1..8]
Chapter 8
P.6
Computer Theory Lab.
8.3 Radix sort
Used by the card-sorting machines you can
now find only in computer museum.
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.
Proof : Choose d = b/r.
IF b < lg n, choose r = b.
  ((b/r)(n+2r)) = (bn/logn)
Chapter 8
P.9
Computer Theory Lab.
8.4 Bucket sort
BUCKET_SORT(A)
1 n  length [ A ]
2 for i  1 to n
3
do insert A[ i ] into
list B  nA[ i ] 
4 for i  1 to n-1
do sort list B [ i ] with insertion sort
6 concatenate B [ 0 ], B [1 ],..., B [ n  1 ] together in
5
order
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
n 1

2 
E[T (n)]  E (n)   O(ni )
i 0


n 1
 (n)   E[O(ni2 )]
i 0
n 1
 (n)   O[ E (ni2 )]
Chapter 8
i 0
P.11
Computer Theory Lab.
We claim that
E[ni2 ]  2  1 / n
We define indicator random variables
Xij = I {A[j] falls in bucket i}
for i = 0, 1, …, n-1 and j = 1, 2,…,n. thus,
ni 
Chapter 8
n
 X ij .
j 1
P.12
Computer Theory Lab.
2
 n

2


E[ni ]  E  X ij  
 j 1  


n n

 E    X ij X ik 
 j 1k 1



n


 E   X ij2    X ij X ik 
1 j  n 1 k  n
 j 1

k j


n
  E[ X ij2 ] 
j 1
Chapter 8
  E[ X ij X ik ],
1 j  n 1 k  n
k j
P.13
Computer Theory Lab.
Indicator random variable Xij is 1 with probability 1/n and 0
otherwise, and therefore
1
 1
E[ X ij2 ]  1   0  1  
n
 n
1

n
When k  j, the variables Xij and Xik are independent, and hence
E[ X ij X ik ]  E[ X ij ]E[ X ik ]
Chapter 8
1 1
 
n n
1
 2.
n
P.14
Computer Theory Lab.
E[ni2 ] 
n
1
1
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
n 1
n 1
2
2
 O( E [ ni ])  O(  E [ ni ])  O(  (1 ))  O( n )
i 0
i 0
i 0
Because
E( ni2 )  Var [ ni ]  E 2 [ ni ]
E [ ni ]  np  1
where p=
Var [ ni ]  np(1  p )  1 
E( ni2 )  1 
1
n
1
n
1 2
1
 1  2   (1 )
n
n
(Basic Probability Theory)
Chapter 8
P.16
Related documents