This preview shows pages 1–2. Sign up to view the full content.
49
26401357
7.9
(a)
Each call to
qsort
costs
Θ(
i
log
i
)
. Thus, the total cost is
n
X
i
=1
i
log
i
=Θ(
n
2
log
n
)
.
(b)
Each call to
qsort
costs
Θ(
n
log
n
)
for length(L)
=
n
, so the total
cost is
Θ(
n
2
log
n
)
.
7.10
A
l
ltha
tweneedtodoisrede
f
ne the comparison test to use strcmp. The
quicksort algorithm itself need not change. This is the advantage of paramer
izing the comparator.
7.11
For
n
= 1000
,
n
2
=1
,
000
,
000
,
n
1
.
5
= 1000
∗
√
1000
≈
32
,
000
,and
n
log
n
≈
10
,
000
. So, the constant factor for Shellsort can be anything less
than about 32 times that of Insertion Sort for Shellsort to be faster. The
constant factor for Shellsort can be anything less than about 100 times that
of Insertion Sort for Quicksort to be faster.
7.12
(a)
The worst case occurs when all of the sublists are of size 1, except for
one list of size
i
−
k
+1
. If this happens on each call to SPLITk, then
the total cost of the algorithm will be
Θ(
n
2
)
.
(b)
In the average case, the lists are split into
k
sublists of roughly equal
length. Thus, the total cost is
Θ(
n
log
k
n
)
.
7.13
(This question comes from Rawlins.) Assume that all nuts and all bolts have
a partner. We use two arrays N[1.
.n] and B[1.
.n] to represent nuts and bolts.
Algorithm 1
Using mergesort to solve this problem.
First, split the input into n/2 sublists such that each sublist contains two
nuts and two bolts. Then sort each sublists. We could well come up with a
pair of nuts that are both smaller than either of a pair of bolts. In that case,
all you can know is something like:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '10
 SHANKER

Click to edit the document details