CS 3100 Proramming Assignment #2 - Computer Science
CS 3100 Proramming Assignment #2 - Computer Science
CS 3100 Proramming Assignment #2 - Computer Science
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>CS</strong> <strong>3100</strong> Programming <strong>Assignment</strong> <strong>#2</strong><br />
Comparing Sort Algorithms<br />
Finished Program DUE Wednesday, October 17 -- you will not turn in a preliminary version of this program.<br />
Write a program that REPEATEDLY<br />
1. prompts the user to enter a filename.<br />
2. opens the file<br />
3. reads a comment from the first line<br />
4. reads an integer N from the second line (e.g. N = 1000)<br />
5. reads N integers from the rest of the file, putting them into<br />
consecutive locations in an array.<br />
6. sorts an identical copy of the array using each of the following<br />
methods:<br />
a. selection sort<br />
b. insertion sort<br />
c. quick sort<br />
d. heap sort<br />
7. writes the name of the input file<br />
8. writes the comment from the input file<br />
9. writes a report of the number of comparisons done in each sort,<br />
and the number of swaps done in each sort.<br />
10. asks the user if he/she wishes to continue<br />
UNTIL the user wishes NOT to continue.<br />
MOTIVATION:<br />
Working on this program and actually experiencing the differences in<br />
runtime is a nice way to get familiar with analysis of algorithms. This<br />
is an important part of the "science" in computer science.<br />
DETAILS:<br />
You need to arrange to pass in a NEW COPY of your original array to<br />
each sorting function, because otherwise the first function will be the<br />
only one that sorts the original list, and the others will just be resorting<br />
a list that is already in order.<br />
When you count "swaps", count the number of calls to the procedure<br />
Swap. (You will be using code I give to you, and that code uses<br />
procedure Swap to do all the data moves.)<br />
When counting comparisons, count ONLY comparisons of the array<br />
contents (the things that are being sorted), not the array indices.<br />
The file sorts2.cpp contain all the sort code you need for this<br />
program. If you wish, you can do this assignment just by adding code<br />
to sorts2.cpp.<br />
Data files are on the class web site:<br />
http://www.cs.csustan.edu/~lamie/cs<strong>3100</strong>/main.htm<br />
SAMPLE INPUT and OUTPUT files<br />
The examples below should be enough to allow you to figure out what<br />
you need to make your program do. Don't believe the counts in the<br />
sample output files. They are just made-up numbers that I thought<br />
seemed plausible.<br />
<strong>CS</strong> <strong>3100</strong> Programming <strong>Assignment</strong> <strong>#2</strong> Page 1 of 2
------------------- sample inputs -----------------<br />
Here is a sample input file we will call ran1000:<br />
This is a random list of 1000 numbers.<br />
1000<br />
233 871 954 23 1245 (... the file continues ... 1000<br />
numbers in all, starting with 233)<br />
Here is a sample file we will call ord50:<br />
This is an ordered list of 50 numbers.<br />
50<br />
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22<br />
23 24<br />
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43<br />
44 45<br />
46 47 48 49 50<br />
------------------- sample output -----------------<br />
File Name: ran1000<br />
File comment:<br />
This is a random list of 1000 numbers.<br />
Here are the results of the sorts:<br />
compares<br />
swaps<br />
selection sort 499500 1000<br />
insertion sort 251000 250000<br />
quick sort 12000 6500<br />
heap sort 12450 7453<br />
File Name: ord50<br />
File comment:<br />
This is an ordered list of 50 numbers.<br />
compares<br />
swaps<br />
selection sort 1225 50<br />
insertion sort 49 0<br />
quick sort 1250 680<br />
heap sort 290 153<br />
TESTING:<br />
Be sure to test your program on large lists (up to 1000 elements or<br />
more), medium size lists, small lists, ordered lists (both ascending and<br />
descending order) and unordered lists. Test all possible combinations<br />
of these list characteristics. This is a very important aspect. It will<br />
determine what you learn from this assignment, and will also<br />
determine a large fraction of your grade.<br />
In the header of your program, include a paragraph or two describing<br />
what you learned from writing and testing this program. Do your very<br />
best to make this well-written. This will be another aspect of the<br />
assignment that will count heavily.<br />
WHAT TO TURN IN:<br />
Turn in the source code, plus a script that shows the full range of the<br />
testing you did. For this program *do not* cat the input files when you<br />
make the script of the tests! Most of the input files will be way too<br />
long for this to be practical. If you write the program according to the<br />
specifications I gave you, and if the comments you put in the input<br />
files are intelligible, then the information printed by the program will<br />
tell me what I need to know about the input files.<br />
Turn in the program source code and the script by email before<br />
midnight Wednesday, October 17.<br />
<strong>CS</strong> <strong>3100</strong> Programming <strong>Assignment</strong> <strong>#2</strong> Page 2 of 2