Professional Documents
Culture Documents
Module03 1up
Module03 1up
Fall 2015
CS240 - Module 3
Fall 2015
1 / 24
CS240 - Module 3
Fall 2015
2 / 24
Crucial Subroutines
CS240 - Module 3
Fall 2015
3 / 24
Selecting a pivot
CS240 - Module 3
Fall 2015
4 / 24
Partition Algorithm
partition(A, p)
A: array of size n, p: integer s.t. 0 p < n
1.
swap(A[0], A[p])
2.
i 1, j n 1
3.
loop
4.
while i < n and A[i] A[0] do
5.
i i +1
6.
while j 1 and A[j] > A[0] do
7.
j j 1
8.
if j < i then break
9.
else swap(A[i], A[j])
10. end loop
11. swap(A[0], A[j])
12. return j
CS240 - Module 3
Fall 2015
5 / 24
QuickSelect Algorithm
quick-select1(A, k)
A: array of size n, k: integer s.t. 0 k < n
1.
p choose-pivot1(A)
2.
i partition(A, p)
3.
if i = k then
4.
return A[i]
5.
else if i > k then
6.
return quick-select1(A[0, 1, . . . , i 1], k)
7.
else if i < k then
8.
return quick-select1(A[i + 1, i + 2, . . . , n 1], k i 1)
CS240 - Module 3
Fall 2015
6 / 24
Analysis of quick-select1
CS240 - Module 3
Fall 2015
7 / 24
i=k+1
CS240 - Module 3
Fall 2015
8 / 24
CS240 - Module 3
Fall 2015
9 / 24
Randomized algorithms
A randomized algorithm is one which relies on some random numbers
in addition to the input.
The cost will depend on the input and the random numbers used.
Generating random numbers: Computers cant generate randomness.
Instead, some external source is used (e.g. clock, mouse, gamma rays,. . . )
This is expensive, so we use a pseudo-random number generator (PRNG),
a deterministic program that uses a true-random initial value or seed.
This is much faster and often indistinguishable from truly random.
Goal: To shift the probability distribution from what we cant control
(the input), to what we can control (the random numbers).
There should be no more bad instances, just unlucky numbers.
CS240 - Module 3
Fall 2015
10 / 24
CS240 - Module 3
Fall 2015
11 / 24
Randomized QuickSelect
Expected cost becomes the same as the average cost, which is (n).
CS240 - Module 3
Fall 2015
12 / 24
Randomized QuickSelect
Second idea: Change the pivot selection.
choose-pivot2(A)
1.
return random(n)
quick-select2(A, k)
1.
p choose-pivot2(A)
2.
...
n
4
i <
3n
4 ,
CS240 - Module 3
Fall 2015
13 / 24
quick-select3(A, k)
1.
p choose-pivot3(A)
2.
...
CS240 - Module 3
Fall 2015
14 / 24
QuickSort
QuickSelect is based on a sorting method developed by Hoare in 1960:
quick-sort1(A)
A: array of size n
1.
if n 1 then return
2.
p choose-pivot1(A)
3.
i partition(A, p)
4.
quick-sort1(A[0, 1, . . . , i 1])
5.
quick-sort1(A[i + 1, . . . , size(A) 1])
CS240 - Module 3
Fall 2015
15 / 24
n1
1X
T (i) + T (n i 1) + (n),
n
n2
i=0
CS240 - Module 3
Fall 2015
16 / 24
CS240 - Module 3
Fall 2015
17 / 24
CS240 - Module 3
Fall 2015
18 / 24
Running time
(n2 )
(n2 )
(n log n)
(n log n)
(n log n)
(n log n)
(n log n)
Analysis
worst-case
worst-case
worst-case
worst-case
average-case
expected
worst-case
CS240 - Module 3
Fall 2015
19 / 24
CS240 - Module 3
Fall 2015
20 / 24
CS240 - Module 3
Fall 2015
21 / 24
Counting Sort
Requirement: Each A[i] satisfies 0 A[i] < k; k is given.
counting-sort(A, k)
A: array of size n, k: positive integer
1.
C array of size k, filled with zeros
2.
for i 0 to n 1 do
3.
increment C [A[i]]
4.
for i 1 to k 1 do
5.
C [i] C [i] + C [i 1]
6.
B copy (A)
7.
for i n 1 down to 0 do
8.
decrement C [B[i]]
9.
A[C [B[i]]] B[i]
CS240 - Module 3
Fall 2015
22 / 24
Radix Sort
Requirement: Each A[i] is a string of d digits xd1 xd2 x0 ,
and each xi satisfies 0 xi < k.
Example: integers between 0 and k d 1
radix-sort(A, d, k)
A: array of size n, d: positive integer, k: positive integer
1.
for i 0 to d 1 do
2.
Call counting-sort(A, k) with each xi as the key
CS240 - Module 3
Fall 2015
23 / 24
Summary of sorting
CS240 - Module 3
Fall 2015
24 / 24