(Last Revision: 10/06/98)

10/06/98 -- changed due date from 10/12 to 10/14

//////////////////////////////////////////////////
CS 3100 PROGRAMMING ASSIGNMENT #02
-- COMPARING SORT ALGORITHMS
//////////////////////////////////////////////////

++++++++++++++++++++++++++++++++++++++++
Finished Program DUE Wednesday, October 14.
(You will not turn in a preliminary
 version of this program.)
++++++++++++++++++++++++++++++++++++++++

Write a program that REPEATEDLY

  1) Prompts the user to enter a filename.
  2) opens the file
  3) reads a comment from the first line
  4) reads an integer N from the second line (e.g. N = 1000)
  5) reads N integers from the rest of the file, putting them
     into consecutive locations in an array.
  6) sorts an identical copy of the array using each of the
     following methods;

      a.  selection sort
      b.  insertion sort
      c.  quick sort
      d.  heap sort

  7) writes the name of the input file
  8) writes the comment from the input file
  9) writes a report of the number of comparisons done in each
     sort, and the number of swaps done in each sort.
 10) Asks the user if he/she wishes to continue

UNTIL  the user wishes NOT to continue.

MOTIVATION:

Working on this program and actually experiencing the
differences in runtime is a nice way to get familiar with
analysis of algorithms.  This is an important part of the
"science" in computer science.

DETAILS:

You need to arrange to pass in a NEW COPY of your original
array to each sorting function, because otherwise the first
function will be the only one that sorts the original list, and
the others will just be re-sorting a list that is already in
order.

When you count "swaps", count the number of calls to the
procedure Swap.  (You will be using code I give to you, and
that code uses procedure Swap to do all the data moves.)

When counting comparisons, count ONLY comparisons of the array
contents (the things that are being sorted), *not* the array
indices.  For example, the line of code:

if (DATA[J] < DATA[J - 1])

contains a comparison that you would have to count, because it
is a comparison of elements in the array.  On the other hand,
the line of code:

while (I < J)

does not compare elements of the array.  It is a comparison of
array indices only.  Therefore we will not count that
comparison.

The files sorts2.p and sorts2.c contain all the sort code you
need for this program.  If you wish, you can do this assignment
just by adding code to sorts2.p or sorts2.c.

Sort2.p is based on the code from the second edition of Dale and
Lily.

Sorts2.c is basically a version of sorts2.p that has been
translated into C using a utility called p2c that runs under
Linux.  To use sorts2.c, you also need to put the file p2c.h in
the same directory, because it is required as an include file in
sorts2.c.  If you write code in C++, you may want or need to
change the name of the file to sorts2.cpp.

You can compile these programs like this on the Ultra's:

g++ sorts2.c

or

pc sorts2.p

I have tested both programs and they appear to work OK when
calling any of insertsort, selectsort, heapsort, or quicksort.
The commented-out calls in these programs worked, so you should
use them as models if you have any doubts as to how your calls
should look.

If you want to call other sorts besides the four I assigned
above, go ahead.  

SAMPLE INPUT and OUTPUT files

The examples below should be enough to allow you to figure out
what you need to make your program do.  Don't believe the
counts in the sample output files.  They are just made-up
numbers that I thought seemed plausible.

------------------- sample inputs -----------------
Here is a sample input file we will call ran1000:

This is a random list of 1000 numbers.
1000
233 871 954 23 1245 (... the file continues ... 1000 numbers in
all, starting with 233)


Here is a sample file we will call ord50:

This is an ordered list of 50 numbers.
50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
46 47 48 49 50

------------------- sample output -----------------

File Name: ran1000

File comment:  This is a random list of 1000 numbers.

Here are the results of the sorts:

                   compares       swaps

selection sort      499500         1000
insertion sort      251000       250000
quick sort           12000         6500
heap sort            12450         7453


File Name: ord50

File comment:  This is an ordered list of 50 numbers.

                   compares       swaps

selection sort        1225           50
insertion sort          49            0
quick sort            1250          680
heap sort              290          153

TESTING:

Be sure to test your program on large lists (up to 1000
elements or more), medium size lists, small lists, ordered
lists (both ascending and descending order) and unordered
lists.  Test all possible combinations of these list
characteristics.  This is a very important aspect.  It will
determine what you learn from this assignment, and will also
determine a large fraction of your grade.

To create pseudo-random lists of numbers, you can use the C
program makelist.c found in this directory.  To compile it,
just type

% cc -o makelist makelist.c

Note: This program needs to be compiled with the cc compiler on
the Sun Ultra's.  The c++ and g++ compilers "do not like" the
call to "random" made in makelist.c.

To call it to make a list of, say, 200 numbers between 0 and
9999, and to cause the list to be written to a new file to be
called list03.b, you type

% makelist 200 0 9999 list03.b

When makelist makes a list of N numbers, it writes N on the
first line of the file.  So in this case, makelist will write
200 on the first line of list03.b, followed by 200 numbers
between 0 and 9999 (inclusive).  You have to give makelist all
the parameters used above, including the filename.

You can prepare sorted lists by using the Unix "sort" command.
For example, if you remove the first line from list03.b, and
then do

% sort -n list03.b > list03.srt

then list03.srt will be sorted in ascending order.  If you do 

% sort -n -r list03.b > list03.srt

then list03.srt will be in descending order.  Don't forget to
put the "N" back into the files you create this way.

In the header of your program, include a paragraph or two
describing what you learned from writing and testing this
program.  Do your very best to make this well-written.  This
will be another aspect of the assignment that will count
heavily.

WHAT TO TURN IN:

Turn in the source code, plus a script that shows the full
range of the testing you did.  For this program *do not* cat the
input files when you make the script of the tests!  Most of the
input files will be way too long for this to be practical.  If
you write the program according to the specifications I gave
you, and if the comments you put in the input files are
intelligible, then the information printed by the program will
tell me what I need to know about the input files.

Turn in the program source code and the script by e-mail before
midnight Wednesday, October 14.

QUESTIONS:

Please bring up in class any questions you still have about
this assignment.