Assignment 5 -- due Tuesday, October 9
at the beginning of lab
CS 4521 Fall Semester, 2012
Topics: Quicksort, randomized quicksort, and their run times
consists of two parts:
In the first part, you are asked to implement quicksort,
randomized quicksort, and heapsort,
and compare their running times with merge sort;.
in the second part, you are asked to prove a fact about the run time
Part 1: Comparing the run times of sorting routines (15 points)
Implement the following three sorting routines:
- heapsort (page 160) --
actually, you can implement "min-heapsort" that sorts in decreasing
order, which is easy to do with the minHeapify() routine of the previous
Rather than implementing accessors and mutators (which is good
software engineering), declare the member variables
(the array A,
heapSize) to be public so that heapsort is not slowed down
by accessor/mutator function calls.
quicksort (page 171), and
randomized-quicksort (page 179).
After you have coded all three of the sort routines, load the array
with the integers 1, 2, ...,
in order, i.e.,
A[i] = i.
This should produce worst-case performance for quicksort; the run times
of the other two sorts should by independent of the order of the input.
Make timing runs for each of the sorts for the largest value of
that you used for merge sort in Assignment 2
and record the times for each of the four sorts:
Compare the results -- which is fastest? slowest?
merge sort (from Assignment 2)
Discussion: for randomized-quicksort,
you can use the C library routine
to get a (pseudo-) random number (int) in the range 0 to
RANDOM(p,r) could just return
p + rand() % (r - p + 1)
-- or, better: change Line 1 of
i = p + rand() % (r - p + 1) ).
What To Hand In:
The results of your timing runs and the
code for each of your new sort routines.
Part 2: Proving a fact about quicksort's run time (5 points)
- (5 points) Exercise 7.4-2, page 184.
Similarly to Section 7.4.1, we want to solve the recurrence:
T(n) = min ( T(q) + T(n - q - 1) ) + Θ(n)
where the minimum is taken over q in [0,n-1].
The problem suggests that T(n) ≥ cnlg(n), which can be shown to be
true by the substitution method for a suitable value of c.
This shows that quicksort is Ω(n lg n) in the best case.
Using the Fact: f(q) = q lg q + (a-q) lg (a-q) attains its
minimum at q = a/2 (and substituting n-1 for a), we have
T(n) ≥ c(n-1) lg((n-1)/2) + Θ(n)
= cn lg(n-1) - c( lower order terms in n ) + Θ(n)
≥ cn lg(n/2) - c( lower order terms in n ) + Θ(n) (for what n?)
= cn lg n - c( new lower order terms in n ) + Θ(n)
≥ cn lg n
for a suitable value of c (how should it be chosen?).
You can assume the Θ(n) term can
be replaced by c1n for some c1 > 0.
Don't forget to prove the Fact.
Page URL: http://www.d.umn.edu
Page Author: Doug Dunham
Last Modified: Monday, 24-Sep-2012 20:39:35 CDT
Comments to: firstname.lastname@example.org