Programming Assignment #3, CPS 100E, Fall 1996:
Sorting Linked Lists (20 points)
Bonus Due Date: Monday, November 4, midnight
Final Due Date: Friday, November 8, midnight
This assignment will provide practice with pointers, lists, and
sorting.
[
Introduction |
Coding |
Grading |
Submitting
]
(A Makefile is accessible in
~ola/cps100e/linksort on the acpub system. Be sure to
create a subdirectory linksort for this problem and to
set the permissions for access by prof/uta/ta by typing
fs setacl linksort ola:cps100e read.)
For net access and outside users files are accessible here:
You'll want to link to the directory containing literary works such as
The Scarlet Letter, The Cask of Amontillado, and
many of Shakespeare's plays. To do this on the acpub system type:
ln -s ~ola/data data
which will create a link to the data directory (the link is named
data.) You can then type data/poe.txt to access
the text by Edgar Allen Poe.
For this assignment you'll read words from a file, store each word in
one node of a linked list, and sort the linked lists using both
quicksort and mergesort. You'll time the two sorting methods using
different input files.
When reading words, you'll store each word once, so if
the word the occurs 20 times in a file
it will appear only once in
the list of words you create. Words are white-space delimited (use the
extraction operator >> to read) and should contain no leading or
trailing punctuation.
back to table of contents
Your program should prompt for the name of a data file, read words from
the file and store them in a list. The code below provides a start.
You don't need to use classes for this program although you are welcome
to do so.
string word,filename;
ifstream input;
cout << "enter file name: ";
cin >> filename;
input.open(filename.cstr());
if (! input)
{
cout << "could not open file: " << filename << endl;
exit(1);
}
while (input >> word)
{
StripPunc(word);
// check if word already in list
// add to front of list if not already in list
}
The function StripPunc is accessible by #including
"strutils.h". You'll probably want to write a function to search
for a word in a linked list.
After reading all the words into a linked list you should sort the list
using Quicksort and time how long the sort takes. The class
CTimer shown below is accessible by #including
"ctimer.h".
CTimer timer;
timer.Start();
QuickSort(first,last);
timer.Stop();
cout << "time to quick sort = " << timer.ElapsedTime() << endl;
(first points to the first node, last points to the
last node.)
To quick sort a list, first partition
using the first node as the pivot node. Partition the
list into two new lists: those nodes less than or equal to the pivot node
and those nodes greater than the pivot node. Then sort the two sublists
and join them together when done. Your code will be more efficient if
you keep pointers to the first and last nodes of the sublists that
result from partitioning since this will make
joining the two lists simpler.
To merge sort a list, find the middle node and divide the list into
roughly equal sized lists. Recursively sort the two lists then merge
the two sublists back into one list by traversing each sorted sublist
and moving the smaller node into the "new" sorted list.
Note that no
new nodes are created once the words are read from the file, only
pointers are re-arranged to sort the lists.
back to table of contents
You should be sure to test your program to ensure that it sorts
correctly. You must also time the program using several data files.
You should explain as best you can the results of the timings. This
means you should try to present reasons for why some files are sorted
faster with each sorting method.
You must provide timings and explanations of the timings in your README
file for the following data files. You can supply additional data if
it helps in evaluating the sorting methods.
- poe.txt
- melville.txt
- hawthorne.txt
- shakespeare/romeo.txt
- shakespaere/hamlet.txt
This assignment is worth 20 points. Points will be awarded as follows:
| Behavior
| Points
|
| Sorts Correctly with Quicksort |
5 |
| Sorts Correctly with Mergesort |
5 |
| Coding Style
|
5 |
| README, Explanation |
5 |
You should create a README file for this and all assignments.
All README files should include your name as well as the name(s)
of anyone with whom you collaborated on the assignment and the amount of
time you spent.
To submit your assignment, type:
submit100e linksort README linksort.cc Makefile ...
Be sure to submit all source files.