Assignment #7: Comparison of Heap Sort and BST Sort
This
assignment is very similar to the previous one. This time you have to program a
heap, and use it for sorting. As we learned in class, heaps are designed
specifically to find the minimum (or maximum) of a data set in guaranteed O(log n), since the heap is always complete (and therefore,
its height is always O(log n)). Heapsort works
exactly like the BST sort you programmed last week, except that you build and
destroy a heap, instead of a BST.
We also
learned that heapsort should be faster than BST sort.
Since you now have both sorts at hand, you have to compare and see if this
assumption is true. The assignment has 2 parts:
Part I:
program a heap data structure to perform heapsort.
Part II:
compare its performance to last week’s BST
About
part I:
There’s
not too much to say, except: do not even attempt to not program the heap in an
array. Or more positive: program the heap in an array. We learned in class how
the indices of children and parents can be computed (2*n+1, 2*n+2 …)
About
part II:
A
straightforward comparison would be:
·
Generate n random numbers, sort them using BST sort, sort the same array
of numbers using heap sort, get the ratio of runtime and print it
·
Do the same with 2n, 3n, 4n, … 20n random
numbers.
For such
a comparison, we need to find a number n that results in quantifiable and reasonable runtimes. The
first task for comparison is to find an appropriate number n:
Find a
number n of random numbers, for which heap sort needs about 1 second to sort.
Take this n as a start value for your comparison program (which then generates
runtime measuring for n, 2n, 3n, .. 20n).
(If you
didn’t finish last week’s assignment, finish this one for heap sort only.)
That’s
it.
Deadline:
Sunday 4/1 for 10 points, one more week for 6 points.