Selected Solutions for Chapter 8:
Sorting in Linear Time
Solution to Exercise 8.13
If the sort runs in linear time for
m
input permutations, then the height
h
of the
portion of the decision tree consisting of the
m
corresponding leaves and their
ancestors is linear.
Use the same argument as in the proof of Theorem 8.1 to show that this is impos
sible for
m
D
nŠ=2
,
nŠ=n
, or
nŠ=2
n
.
We have
2
h
±
m
, which gives us
h
±
lg
m
. For all the possible
m
’s given here,
lg
m
D
.n
lg
n/
, hence
h
D
.n
lg
n/
.
In particular,
lg
nŠ
2
D
lg
nŠ
N
1
±
n
lg
n
N
n
lg
e
N
1 ;
lg
nŠ
n
D
lg
nŠ
N
lg
n
±
n
lg
n
N
n
lg
e
N
lg
n ;
lg
nŠ
2
n
D
lg
nŠ
N
n
±
n
lg
n
N
n
lg
e
N
n :
Solution to Exercise 8.23
The following solution also answers Exercise 8.22.
Notice that the correctness argument in the text does not depend on the order in
which
A
is processed. The algorithm is correct no matter what order is used!
But the modified algorithm is not stable. As before, in the final
for
loop an element
equal to one taken from
A
earlier is placed before the earlier one (i.e., at a lower
index position) in the output arrray
B
. The original algorithm was stable because
an element taken from
A
later started out with a lower index than one taken earlier.
But in the modified algorithm, an element taken from
A
later started out with a
higher index than one taken earlier.
In particular, the algorithm still places the elements with value
k
in positions
CŒk
N
1±
C
1
through
CŒk±
, but in the reverse order of their appearance in
A
.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document82
Selected Solutions for Chapter 8: Sorting in Linear Time
Solution to Exercise 8.33
Basis:
If
d
D
1
, there’s only one digit, so sorting on that digit sorts the array.
Inductive step:
Assuming that radix sort works for
d
N
1
digits, we’ll show that it
works for
d
digits.
Radix sort sorts separately on each digit, starting from digit
1
. Thus, radix sort of
d
digits, which sorts on digits
1; : :: ; d
is equivalent to radix sort of the loworder
d
N
1
digits followed by a sort on digit
d
. By our induction hypothesis, the sort of
the loworder
d
N
1
digits works, so just before the sort on digit
d
, the elements
are in order according to their loworder
d
N
1
digits.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 .
 LG, lg n lg, lg nŠ lg, lg D lg

Click to edit the document details