
(Last Revision: 12/09/97)
(12/09/97-corrected a typo item #9 below.)

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

++++++++++++++++++++++++++++++++++++++++
Finished Program DUE Monday, November 24.
(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.

DETAILS:

You will probably want to either make the list a value
parameter of each sort procedure, or pass in a copy of the
original list to each sort.  Why?  

The second alternative given above is better than the first.
Why?

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, "if DATA[J] < DATA[J - 1]" contains a
comparison to be counted, but "while I < J" does not.

The files sorts2.p and sorts2.c contain all the sort code you
need for this program.  All you have to do is pick one of the
files and augment it.

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

Sort2.c is basically a version of sort2.p that has been
translated into C using a utility called p2c that runs under
Linux.  To use sort2.c, you also need to put p2c.h in the same
directory, because it is required as an include file in sort2.c.

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), medium size lists, small lists, ordered lists (both
ascending and descending order) and unordered lists.  Test all
possible combinations of these list characteristics.

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

% cc -o makelist 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 describing
what you learned from writing and testing this program.  

WHAT TO TURN IN:

Turn in the source code, plus a script which shows the full
range of the testing you did.  For this program *don't* cat the
input files when you make the script of the tests!  If you
write the program according to the specifications I gave you,
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 Monday, November 24.
