Deterministic Selection and Sorting:: Problem P
Deterministic Selection and Sorting:: Problem P
Problem P
Algorithms
Professor John Reif
fi divide into
subproblems
size n
solve these and "glue" together
solutions
k
T(n) = T(ni ) + g(n)
i=1
ALG 1.3
Deterministic Selection
and Sorting:
,...,n
Examples:
1s t lecture' s mult
M(n) = 3 M
F(n) = 2 F
+ q(n)
( )
+ q(n)
B(n) = B
merge s orting
S (n) = 2 S
n
2
n
2
( )
binary search
n
2
n
2
+ q(1)
( )
+ q(n)
2
Examples
Time =
on
( #oncomparisons
longest path )
input a,b,c
Binary tree
a<b?
root
Y
b<c?
Y
a,b,c
facts:
a<c?
b,a,c
a<c?
Y
b<c?
N
Y
a,c,b
with L Leaves
(1) has = L-1 internal nodes
(2) max height
log L
c<a<b
b,c,a
N
c,b,a
Merging
Algorithm Insert
input
( X1 < X 2
and
Case k=n-1
goal
provably asymptotically optimal
algorithm in
[2] i f Y1 > X
k + 1
(
5
< . . . < Xk
else Y1 X
of lower bounds
allows simple proofs
so easy
time = # comparisons
to bound time costs
insert Y 1 into
k
2
k
2
X1 < . . . < X k
2
Input
log (k+1)
log (n)
w h e re k =
n- k
n
2
Algorithm
[1] i 1, j 1
[2] while
i k and j
k do
i f Xi < Yj t h e n ou t p u t ( Xi ) an d s e t i i +1
e l s e ou t p u t ( Yj ) an d s e t jj+1
[3] output
remaining elements
n-1 comparisons
8
Sorting by Divide-and-Conquer
Algorithm
Lower bound:
input
consider case X
1 < Y1 < X 2 < Y2 <...< X k < Yk
any merge algorithm must compare
Merge Sort
set S of n keys
claim:
n
[1] partition S into s et X of
2
n
keys
and s et Y of
2
flip
keys
n
2
n
2
[4] output
merged sequence
10
Time Analysis
( ) ( )
T(n) = T
n
2
+ T
T(l) = 0
fi T(n) = n
log n
n
2
-2
log n
+ n-1
+ 1
d epth log (n !)
= q (n log n)
.
...
n! distinct leaves
11
12
Easy Approximation
(via Integration)
n -1
k -1
Selection Problems
log x dx )
input
output
Better bound
using
pn
2p
() (
n
e
e {1,...,n}
Sterling Approximation
n
n!
X1 , X 2 ,..., X n
and index k
1+
1
n
12
1
pn)
log (2p
2
13
14
History:
because:
..
(1)
- although winner X
always gets lst prize
- second X (2) may not get 2nd prize
X (1)
X(1)
X(2)
n-1
- X (1) requires
X (2)
n-1 comparisons
proof
everyone except the champion X(1)
must lose at least once!
n-1 comparison
...
X(1)
X (1)
15
16
n-2 +
logn
X(2)
comparisons
|Y| logn
X(1)
Algorithm
[1] form a balanced binary tree
for tournament to find X
using n-1 comparisons
[4] output
x
x
x
using
(1)
logn
-1 more comparisons
Y
(1)
log n
height
(1)
x (1)
17
18
n-2 +
X (2)
logn comparisons
(2)
proof
#comparison
where m
m 1 + m 2 + ...
19
20
declare
lemma
(#who lost to X
(1)
Xi > Xj if
) logn
in worst case
(a) X previously undefeated and
i
X j lost at least once
(b) both undefeated but Xi played
more matches
proof
Use oracle who "fixes" results
of games so that champion X
plays logn matches
21
(1)
to root
length logn
22
Selection by Divide-and-Conquer
(k)
by sorting X
[ 2] divide X into
Algorithm
Select
n sequences
d
of d elements each (with < d leftover),
and sort each sequence
(X)
input
[5] let X
let X
|M|
2
of each of
(M)
= {x e X | x < m}
= {x e X | x > m}
[6] if |X
else
23
output
Select
k-(n - |X |)
(X )
m
+
(X )
24
Columns
smallest
sequences
n
d
n ot X
M in sorted order
d
n ot X
(assuming d is constant)
If say d=5,
largest
d+1
Propos ition |X |, |X | each n - 2
T(n)
c1
c1
T
T(n)
20c n = O(n)
1
n
2d 3 n
if n < co
( )
n
d
+ T
( )
3
n
4
+ c1n
25
26
proof
Fix a path p from root to leaf
The comparisons done on p define a
relation R p
+
X = {x
,..., x
n },
Lemma
(k)
index k
proof
m either x i Rp x m or x m Rp x i
then for all i
Suppose x i is un related to x m by Rp
n-1
for x i be when
j where either
(1) j=m
+
(2) x i Rp x j and x j Rp x m , or
+
(3) x j Rp x i and x m Rp x j
Fact
27
28
n
(1) D 2
X1 ,..., X n
increment
sort
begin
j i- D
while j>0 do
if x j > x
j+ D
then
begin
SWAP (x j , x
j j- D
j+ D)
end
else j 0
end
D D/2
29
30
procedure
increment sort (Y
passes of SHELLSORT:
1
increment s ort
increment s ort
(
(
Xk , X
n
+ k
2
Xk , X n
4
+k
for k=1, . . . ,
, Xn
2
+k
, X3
4
n
2
n+ k
facts
,..., Y
n
for k=1, . . . ,
4
sorted in pass p
2
+ 1
31
(n )
1. 5
32
procedure RADIXSORT
input
[1]
X1 , ... , X n e {1 ,..., n}
-- avoids
[2]
[3]
[4]
output
X i 1 X i 2 ...
by avoiding comparisons
instead uses indexing of RAM
-- generalizes (in c passes) to key
domains {1 ,..., n c }
X in
33
34
(1)
Complexity of SHELLSORT
-- very good in practice
claims Sedgewick
-- Is it
(2)
q(n 1.5 )?