Comp 200
Symonds Day
01.feb.28

Today we'll be comparing different algorithms for the same task, by looking at the running-time differences. You'll want to start up DrScheme, so you can actually try the examples.

In class, we've seen two different ways of sorting numbers, insertion sort, and mergesort. For a list of n numbers, we argued that insertion sort (``isort'') takes roughly proportional to n2 steps (worst case); mergesort (``msort'') takes roughly n log(n) steps. In today's lab we put on our lab jackets and safety goggles, and actually run some timing experiments, checking whether the results jive with our theoretical analysis.

Settings

After firing up DrScheme, a couple of small things: Press Execute, to make sure these selections are enabled.

Making Test Lists

In order to compare these different sorting algorithms, let's create some large lists of numbers to try them on. You are provided with functions nums-down, nums-up, nums-rand, which each take a single number and return a list of that length. (First try some small examples, and then create some large examples, giving names to those lists -- e.g., (define up100 (nums-up 100)).)

Timing

You can time a function call, say, (mSort (list 8 1 3 2)) by writing (time (mSort (list 8 1 3 2))). Of course, if you have a placeholder like up100, you could (time (mSort up100)). (Why do we really want to make the list in advance, and when doing the timing just use the placeholder?) It gives three numbers; the interesting one is the "cpu time", which is in milliseconds.

There is a slight inconvenience here, in that for our purposes we don't really care about the result of mergesort; we only care about the time information. Yet the result spews forth, taking up many lines of the screen. Instead, you can (time (empty? (mergesort up100))); presuming that empty? takes a negligible amount of time to run, this helps us keep our display readable.

Try running (time (empty? (mergesort up100))) several times in a row. Do you always get the same answer? If not, how do we come up with a "right" answer?

Today's Task

We will complete the following table with actual data. You will be divided into three teams, one for each row. Within a team you will be responsible for organizing yourselves to measure each cell. When you have an answer, bring it to the instructor, who will fill in the table.
input size
400 800 1600
Insert sort
up 3
down324
rand160
up 6.6
down1655
rand148
up 9
down7506
rand3237
Mergesort
up 28
down32
rand30
up 86
down76
rand68
up 145
down160
rand175
Quicksort
up 782
down921
rand40
up 3451
down3599
rand93
up 12445
down13798
rand215

Natural questions to ask

As a final exercise, recall the functions doublerA, doublerB. How many steps did each of those need to compute their answer? Try timing them on various inputs. What is the largest value of n for which (doublerB n) finishes in less than 30 seconds? A minute? What is the largest input, do you predict, for which doublerB could finish in less than a year (which happens to be 525960min, or about 219 min)?

n (time (doublerA n)) (time (doublerB n)) (time (max-of-list (nums-up n))) (time (max-of-list (nums-down n))) (time (max-of-list (nums-rand n)))
10
12
14
16
18
20
Examining the code for these functions, can you explain the timing results?
Back to the Comp 200 home page.