CSCI-20 Lab 5 – Sorting, due 4/19/16

advertisement
CSCI-20 Lab 5 – Sorting, due 4/19/16
Please work in groups of two on this lab. Create two large arrays of ints, perhaps 500,000 or more,
but at least as big as possible on your system. The arrays may compile but not be able to load, so
you might need to stop unneeded processes before loading your program. Write sort functions
implementing (at least) the sorting algorithms discussed in class:








heapsort
mergesort
quicksort
shellsort
bubblesort
ripplesort
insertionsort
selectionsort
Fill the first array with random numbers. I have put a good random number generator on my Web
site. Then repeatedly copy varying length segments of the data into the second array and sort the
sections of the second array, using each of the various sort functions, timing each sorting operation.
Use a millisecond timer such as used in the quicksort.cpp example. Remember the problem of
graininess of the timer. Too-small array sections may give zero millisecond sort times.
Collect a table of statistics for the time each sort takes for each length of array being sorted. Make
sure you run enough randomization and sort passes each time to be sure that the times involved are
relatively reliable (this may take a few dozen or even a few hundred passes on a fast computer).
For quicksort, write two versions, one using the first element in each sub-array for the pivot and one
using a randomly selected pivot (or you can pass a parameter saying which pivot to use). Time both
versions with both random and already-sorted data. The easiest way to do this is to just call
quicksort again on the already sorted data. Keep the statistics for both versions.
When you have finished collecting the timing data, print a table with sort type as the vertical axis
and average timing for each size of the data sets as horizontal axis, something like this:
Shell
Quick(1)
Quick(2)
Heap
(etc.)
Num Elements
sort times
mmm
xxx
ttt
ccc
1000
in ms...
nnn
ooo
yyy
zzz
aaa
bbb
ddd
eee
10000
100000
Your table will have more columns, one for each array test size. Be sure you have enough test data
for each sort to get a good idea of the relative run times of each sort. Print the sort times formatted
into clear columns. Verify empirically that the sorts obey the big-Oh execution times that we
showed they would in class. Discuss your analysis regarding the sorts with me in lab.
Download