Você está na página 1de 2

CPS 130 Homework 6 - Solutions

1. (CLRS 7.4-5) The running time of Quicksort can be improved in practice by taking
advantage of the fast running time of Insertion-Sort when its input is \nearly" sorted.
When Quicksort is called on a subarray with fewer than elements, let it simply
return without sorting the subarray. After the top-level call to Quicksort returns,
run Insertion-Sort on the entire array to nish the sorting process. Argue that this
sorting algorithm runs in ( + lg( )) expected time. How should be picked, both
in theory and in practice?
Answer: A complete proof would be similar with the proof of the average case running
time of Quicksort (CLR section 7.4.2).
The main idea is to note that the recursion stops when 2i = , that is = log2 . The
recursion takes in total (  lg ). The resulting array is composed of subarrays of size
, where the elements in each subarray are all less than all the subarrays following it.
Running Insertion-Sort on the entire array is thus equivalent to sorting each of the
subarrays of size , which takes on the average  ( 2) = ( ) (the expected running
time of Insertion-Sort is ( 2)).
If is chosen too big, then the ( ) cost of insertion becomes bigger than ( lg ).
Therefore must be (lg ). Furthermore it must be that ( + lg ) = ( lg ). If
the constant factors in the big-oh notation are ignored, than it follows that should be
such that lg which is impossible (unless = 1) - the error comes from ignoring the
constant factors. Let 1 be the constant factor in quicksort, and 2 be the constant factor
which requires
in insertion sort. Than must be chosen such that 2 + 1 lg
1 lg
lg
.
In
practice
these
constants
cannot
be
ignored
(also
there
can
be lower order
1
2
terms in ( lg )) and should be chosen experimentally.
2. (CLRS 7-3) Professors Dewey, Cheatham, and Howe have proposed the following \elegant"
sorting algorithm:
Stooge-Sort(
)
if [ ] [ ]
then exchange [ ] $ [ ]
if + 1 
then return
b( + 1) 3c
Stooge-Sort(
)
Stooge-Sort(
+ )
Stooge-Sort(
)
k

O nk

n=k

O n

n=k

O k

O nk

O n

O nk

O nk

n
k

O n

k <

c k < c

c k

n
k

< c

O n

A; i; j

A i

> A j

A i

A; i; j
A; i

A; i; j

a.

A j

k; j

Argue that Stooge-Sort( 1


where =
[ ].
Solution: By induction:
A;

length A

[ ]) correctly sorts the input array [1 ],

; length A

::n

For the base case let = 2. The rst two lines of the algorithm will check if
the two elements are sorted; if not, it exchanges them (and now they are sorted).
The algorithm returns after the following if statement. Thus Stooge-Sort sorts
correctly for = 2.
Assume Stooge-Sort correctly sorts an input array [1 ], where =
[ ]
and 1 
. In particular, Stooge-Sort correctly sorts an input array of size
= 2 3 (you may also assume Stooge-Sort sorts correctly for 1
= 2 3).
Let [1 ] be an input array of size =
[ ]. By the induction hypothesis
the rst call to Stooge-Sort(
) correctly sorts the rst 2 3 elements,
so that the elements 1
3 are less than elements ( + 1) 3 2 3. The call
to Stooge-Sort(
) correctly sorts the last 2 3 elements, so that the
elements ( + 1) 3 2 3 are less than elements 2( + 1) 3 , which are the
largest 3 elements in . The last call to Stooge-Sort(
) sorts correctly
(by induction hypothesis) the sorted elements are less than elements 2( +1) 3 .
Thus the array of size is sorted.
Give a recurrence for the worst-case running time of Stooge-Sort and a tight
asymptotic (-notation) bound on the worst-case running time.
n

::k

length A

k < n

n=

< k

::n

A; i; j

n=

: : : n=

A; i; j

n=

:::

:::

n=

n=

n=

:::n

A; i; j

b.

n=

length A

:::n

Solution:

( ) = 3 ( 23 ) + (1)
= ( log = 3)
= ( 2 7 )

T n

n
n

c.

3 2

: :::

Compare the worst-case running time of Stooge-Sort with that of InsertionSort, Merge-Sort, Heapsort, and Quicksort. Do the professors deserve
tenure?
Solution: Stooge-Sort is the worst of all the algorithms { the professors do not
deserve tenure.
Insertion-Sort: ( 2 )
Merge-Sort: ( lg )
Heapsort: ( lg )
Quicksort: ( 2 )
Stooge-Sort: ( 2 7 )
n
n

n
n

: :::

Você também pode gostar