(Latest Revision: 08/14/2003)
Using Recursion to Sort a Sequence of Integers
Discussion of Selection Sort
Suppose you have a sequence of one or more integers, take this sequence for example:
168 335 797 288 142 941 117 884 464 448
The idea of a selection sort is to follow this algorithm:
=================
Algorithm 1:
IF the sequence to be sorted consists of more than one number then
- find the position of the smallest number in the sequence,
- swap the smallest number in the sequence with the first
number in the sequence, and
- start this algorithm again, except working with the second
through last element of the original sequence.
ELSE (the sequence to be sorted consists of just one number) so just
stop -- you are finished.
=================
To illustrate, the sequence above changes first from
168 335 797 288 142 941 117 884 464 448
to
117 335 797 288 142 941 168 884 464 448
We then "take it from the top", except now working with the last 9 elements
of the 10-element sequence. We find that the 142 is the smallest number in
that sequence, and we swap it with the 335, thus arriving at this sequence:
117 142 797 288 335 941 168 884 464 448
Next we take it from the top again, this time working with the last 8
elements of the sequence. We find that 168 is the smallest element and
swap it with the 797, thus getting this sequence:
117 142 168 288 335 941 797 884 464 448
This continues on. We get the following progression of sequences:
117 142 168 288 335 941 797 884 464 448
117 142 168 288 335 941 797 884 464 448
(Those two were "degenerate" since we merely swapped numbers with
themselves twice.) Next we get:
117 142 168 288 335 448 797 884 464 941
117 142 168 288 335 448 464 884 797 941
117 142 168 288 335 448 464 797 884 941
At this point, we check the sequence 884 941, and just swap 884 with
itself. After that, we have just the one-element sequence 941 to "sort"
and there is nothing left to do.
The Assignment:
Your assignment is to write a program that contains a function that uses
the idea of Algorithm 1 above to recursively sort a sequence of one
or more integers.
A sample run of such a program is included in this directory. You can view
it by clicking
here.
INPUT:
You must write your program so that it gets all its input from standard
input.
Whether or not you are aware of it, undoubtedly you have already written
many programs that read from standard input. You do this simply through
the use of ordindary 'cin statements.'
The keyboard is the default standard input, but standard input can be
conveniently 'redirected' to make the program read from a file instead of
the keyboard by using the unix '<' symbol. See the
sample run
for examples of how unix redirection is used to make a program read from a
file instead of the keyboard.
As you see from reading the sample run, when the user types recSort
the 'cin statements' in the program named recSort just read input from the
keyboard. The user must use the keyboard to enter the sequence of numbers
to be sorted (In this case the input is the short sequence: 123 84).
When the user types
recSort < input02
the 'cin statements' in the recSort program are redirected to read from the
file called input02 instead of the keyboard. That way the recSort
program reads data the user has prepared in advance. The user need not
retype a long sequence of input when s/he repeats a test.
You must write the program so that it is able to input and sort a sequence
of any length between 1 and 100 (inclusive). The program must copy the
sequence from the standard input into an internal array and then sort the
internal array.
The function that does the sorting must input (as parameters) the internal
array and two integers denoting the first and last indices of the section
of the array that is to be sorted.
Here is sample code that shows how to declare the max capacity of the array
and give a prototype for the sorting function:
/* The capacity of the internal array */
const int MAX_ELTS = 100 ;
/* Recursive function that sorts a section of the elements in
an array */
void sort (int list[MAX_ELTS], int firstIndex, int lastIndex) ;
The sorting function must be a recursive function. Also, this
function must print the messages you saw in the
sample runs
such as:
Sorting from 0 to 9.
Sorting from 1 to 9.
Sorting from 2 to 9.
... and so on.
To get those messages, you just put a cout statement at the very beginning
of the body of the function. The cout statement writes the values of the
actual parameters, firstIndex and lastIndex. Because the function is
recursive, the user sees a report on the parameter values for each level of
the recursion. We make the program print these messages to aid our
understanding of what happens during execution.
I can answer your questions and give more details concerning the
information above in class.
OUTPUT:
Your program must write all its output to standard output. (Use ordinary
'cout statements' to achieve this.)
As you can see from examining the
sample runs
of the program, your program is required to write its messages and then
write the sorted sequence to the standard output. Your program must format
the output so that
- there is a tab character between successive numbers on the same line,
and
- the program always starts a new line of output after it has written
ten numbers on the current line.
I plan to test your program by using a script, in this manner:
a.out < input03
a.out < input04
a.out < input05
a.out < input06
a.out < input07
a.out < input08
The names input03, input04, and so on denote files I will have in my
directory. Each file will contain a different sequence for your program to
sort. The script above just executes the program over and over, taking its
input from each of the input files in turn.
Your program has to work correctly under my test conditions or you will
lose significant credit. To make sure everything will work, make some test
input files of your own and try out your program on them.
What To Turn In:
This is not going to be a large program. It will probably be a level-three
program with only three or four functions, including the main function.
Therefore, I am not requiring you to turn in a preliminary version.
You will turn in two printer outputs (hardcopies) and you will send me one
e-mail message. Please follow these rules:
- Always send me e-mail as plain text in the main message body. Never
send me attachments.
- Always use the exact subject line I specify for each message.
(I often get hundreds of e-mail messages in a week. The subject line
allows me to find, filter and sort messages.) You will lose a
significant number of points on the assignment if you use the wrong
subject line.
- Be very careful when typing the command to send e-mail. You may use
the instructions in your
Hello World! lab excercise
for guidance. Of course, you will need to make the obvious changes to
those directions -- you have to use the correct subject line and
filename.
- Always send yourself a copy of each e-mail message you send to me, and
check immediately to see if you receive the message intact.
You are responsible for sending e-mail correctly.
Here is the list of things you have to turn in:
- At the start of class on the due date, place the
following items on the "counter" in front of me:
- a hardcopy of your final version of the program source code,
and
- a hardcopy of your test script showing adequate testing of your
program.
Make sure that all of the code and script content shows on the paper.
Make sure all content is plainly readable and properly formatted.
- Send the following item to me by e-mail before midnight on the
due date:
Your
final version of the source code,
with subject line: CS2500,prog2.f.
Note that there are no spaces in the subject lines given above. It is
important that you do not insert any spaces. My e-mail address is:
john@ishi.csustan.edu .
DUE DATE:
For the due date, see
the class schedule.