Advanced Placement Computer Science AB:
Assignment 9:  Extra Credit:  N2 Behavior of SelectionSort
    Sewickley Academy, 2000-2001

See Course Home Page.
 
Date Assigned: Wed Oct-4
Date Due: Thu Oct-5

This is optional -- you do not have to do it.  Of course, it would help
your grade....  Plus, it's a cool assignment, if I may say so myself.
:-)

Part 1:  Selection Sort

Your problem here is to first ask for a length from the user
(repeatedly, halting when user enters 0, like we always do), and then
generate a vector of that length full of random integers.  Next, you
should *sort* the integers from smallest to largest.  You will do this
by the following algorithm, written in pseudocode (ie, not in C++ , of
course):

    For index = 0 to maxIndex
        Find smallest remaining element (from index to maxIndex)
        and swap that element with array[index]

Part 2:  Verifying the Time Complexity of this Sort

This is a quadratic sort, also written O(n*n).  This means that each
time you double the size of the vector, you should expect the sort time
to grow by a factor of 4.  Your job here is to verify this.  How?
Easy.  No longer use the user input from above.  Instead, generate
vectors long enough to take, say, 5 seconds, 10 seconds, 20 seconds and
40 seconds to sort.  Call the number of elements required in these
vectors n1, n2, n3, and n4.  Here is what you do:  for each of n1, n2,
and n3, randomly generate 3 vectors of that length, time how long it
takes to sort them (with a stopwatch, as accurately as you can -- we'll
do it with system time later), and plot (on graph paper) the average of
these 3 runs for that value of n.  Now, you should see a very clear
*parabola* emerge.  The way to prove this is to write the values for
Ni/Ni-1 (so, n2/n1, n3/n2, n4/n3) and see that they are very nearly 4.

Good luck.

DK



See Course Home Page.