(Latest Revision: 09/17/2000)
Radix Sort: Using Queues and Structured Data
RATIONALE:
Writing this program will give you some practice using queues
in a non-trivial application. It will also give you an
opportunity to invent additional data structures to suit the
needs of the problem at hand.
THE ASSIGNMENT:
Write a program that
- implements a queue data type whose elements are
arrays of six characters,
- implements a set of ten of the
above-described queues, giving random access to each
queue in the set, and
- uses the queue set to do a radix sort of a non-empty
series of six-digit non-negative integers.
INPUT:
The input will consist of a series of one or more lines of
text. The text on each line will consist of one six-digit
integer, left justified. Each integer will be non-negative.
Each integer will have exactly six digits -- no more, no less.
The program will read all input from standard input.
SAMPLE INPUT:
789525
840988
220231
453213
792950
993727
000289
444569
315931
613484
744813
OUTPUT:
The program will sort the numbers into ascending order and
print the sorted list to standard output. The output will be
formatted identically to the input.
SAMPLE OUTPUT:
000289
220231
315931
444569
453213
613484
744813
789525
792950
840988
993727
HOW THE USER WILL OPERATE THE PROGRAM:
Suppose the executable program is called rsort. If
the user wants to enter the input from the keyboard, s/he just
enters the command:
rsort
and then enters the desired series of integers. When finished
entering data the user presses the enter key and makes a
control-D to signal the end-of-input.
Suppose the user wants the input to come from a prepared file
called input01. In that case s/he just enters:
rsort < input01
In this case, where redirection of standard input to a file is
done, there is no need to enter the end-of-input signal.
The user may wish to save the output of the program to a file.
In that case, s/he can use this command:
rsort < input01 > output01
When I test your program, I will test it on several prepared
files. Some input files will contain a large quantity of
integers to sort. Other files will be small.
THE ALGORITHM:
There are ten queues: queue[0], queue[1], ..., queue[9].
Each input integer is a six-digit number. The radix sort makes
one pass for each of these six digits.
The first pass sorts according to the least significant digit
(ones-digit). In this pass, the program inserts each number
into the queue corresponding to the number's least significant
digit. In the 5 succeeding passes the program takes numbers
off the queues and re-queues them according to their
"tens-digit", "hundreds-digit", and finally their most
significant digit ("hundred-thousands-digit"). When it
performs one of these passes the program first takes all the
elements from the previous pass off queue[0] and re-queues
them. It then does the same thing for queue[1], queue[2], and
so on, up to queue[9]. After the sixth and final pass, the
program dequeues the numbers and writes them to the output.
They come out in sorted order. (If you are "still confused"
you can look at
this example.
I'll discuss the example in class.)
Other than the queues described above, the program need not
allocate any storage to hold the list of integers to be
sorted. Immediately after inputting each integer, the program
may enqueue it according to the value of its ones-digit. After
the last pass is complete the integers are in the queues,
distributed according to their hundred-thousands-digit. The
program can then write the sorted list to the standard ouput
merely by emptying the queues, starting at the 0-queue and
proceeding in numerical order to the 9-queue.
One element is still missing from the description of the
algorithm. Since the program takes integers out of queues and
re-enqueues them into the same set of queues, we enqueue a
marker in each queue before beginning a new pass. When the
program dequeues a marker, it knows that it has finished
dequeueing all the integers that were in that queue at the
start of the pass. For the marker, you may choose any
six-character array that cannot be confused with a list
element. For example, the array zzzzzz could be the marker.
IMPLEMENTATION OF THE NUMBERS AND QUEUES:
The program must read each six-digit integer as a sequence of
six characters, not as an integer variable. The program must
internally represent each six-digit integer as an array of six
characters. The program has to select individual digits from
each integer as part of the sort. The value of the digit
determines which queue the integer will be inserted into next.
When we represent the integers as arrays of characters, it
makes it particularly easy to perform this digit selection.
Use a linked list implementation of the queue data type. I
recommend that you use the implementation in our text book.
The
header
and
implementation
files are in this directory. You
will have to modify this code slightly to make the queue
element type an array of six characters.
Since there are ten related queues to be implemented, they must
be organized into an appropriate data structure. An array of
ten queues will work well.
ADDITIONAL SPECIFICATIONS:
Before you do any assignment for me, you need to read the
programming assignment rules.
This document contains my general rules regarding form, and
style. The document also describes my grading criteria.
You also need to reference the other documents here:
http://shalim.csustan.edu/~john/Classes/General_Info/progAsgRules/
to make sure you are correctly applying the top-down design
methodology and to make sure that you are including everything
necessary in the programs and scripts you send me.
HELP WITH TESTING:
There is a
makeList program
in this directory that you can use to generate lists of
integers to sort.
When you test the algorithm on a long list of integers you can
use the unix sort and diff commands to verify
that your program sorted the list correctly.
For example, suppose that you have a file called data
containing 1000 integers in random order. Suppose that the
name of your executable radix sort program is rsort.
if you execute the following commands
rsort < data > myoutput
sort -n < data > sortoutput
diff myoutput sortoutput
then rsort sorted the numbers correctly if and only if
diff found no differences between myoutput
and sortoutput. (When there are no differences
between the files, diff has no output at all, or
possibly just some blank lines.)
I expect to see your use of rsort and diff in
the test scripts you make.
MORE HELP:
Come to class to get help and hints.
WHAT TO TURN IN:
You will be sending me two e-mail messages. Please follow
these rules:
- Always send me e-mail as plain text in the main
message body. Never send me attachments.
- I will tell you what subject line to use with each
message, and I need you to use exactly the
subject lines I give you. (I get hundreds of e-mail
messages at a time and your subject line allows me to
sort messages.)
Here is the list of things you have to turn in:
- Send the following items to me by e-mail before
midnight on the first due date:
A
level two version of the source code
and
a script
showing your test runs. Include all the source
files (*.h files and *.cpp files) that are required
to compile the program into an executable image --
everything I need so that I can compile and test
your program. The script must demonstrate adequate
code coverage and data coverage. Combine all the
source files and the script into one
shell archive file
and e-mail me the archive file with the subject
line: CS3100,prog2.2.
- Send the following items to me by e-mail before
midnight on the second due date:
A
final version of the source code
and
a script
showing your test runs. Include all the source files
(*.h files and *.cpp files) that are required to
compile the program into an executable image --
everything I need so that I can compile and test your
program. The script must demonstrate adequate code
coverage and data coverage. Combine all the source
files and the script into one
shell archive file
and e-mail me the archive file with the subject line:
CS3100,prog2.f.
Note that there are no spaces in the subject lines given. It
is important that you do not insert any spaces. My e-mail
address is:
john@ishi.csustan.edu.
DUE DATES:
For due dates, see
the class schedule.