Thursday February 12, 2004
Program due Wednesday 2/18/2000
pseudo-code to be handed in on Thursday 2/19/2000, 10:30am (before class).
Your task is to write in C++ three programs implementing variations on two sorting
algorithms. The first two programs are based on Selection Sort and
should be named sort1.cpp and sort2.cpp, the
third implements Insertion Sort and should be named sort3.cpp.
Each program should contain all the necessary functions and the
main program in one file, named as indicated. You will submit
them in hw3, together with a typescript file.
For Insertion Sort you will also have to submit
handwritten pseudo-code, which is due on Th Feb. 19, at 10:30am
To make the task of testing and grading this homework easier for
me and the TA, your programs should have a very basic user
interface. Without prompting the user, they will just read the
input as a sequence of positive elements separated by spaces and ending
with a special element, which should not be part of the
array to be sorted. For integer arrays, the special element
should be 0 (the zero integer), for character arrays it should be
a .(dot character) and for CustRec arrays, it should be 0 (assuming that no id
of any customer is 0).
The output should appear on a
separate line, with the values separated by spaces.
The internal array(s) used for all these programs should have a
size of at least 25 elements.
The typescript file should be named exactly like this, and
should be short and readable - in particular, it should
NOT contain any junk from emacs executions. It should show
just the compilation and execution of these three programs, one
after another, on appropriately chosen input arrays.
Please remember the rules for structuring your program that I
have given you, make your code readable and be
generous with comments. No late submissions will be
This program is based on the Selection Sort program we designed
in class on Tuesday. Your task is to implement the algorithm
for sorting an array of records of type CustRec (defined in
Lecture 6). Since the CustRec structure has two fields, you can sort by
any of them.
The main program calls a
function to reads the input array from the user, then sorts it by each field and
prints it each time on the standard output (terminal).
This program is similar (almost identical!) to sort1.cpp,
except that this time the sorting function should be implemented
as a template. The main program should test the sorting algorithm
on two sets on inputs, first on an array of integers, then on an
array of characters. The input functions obey the rule of simple
interface described above (whereas an array is entered as a
sequence of elements ending with the special element), and should
be invoked in the order: integers first, then characters. For
example, a possible data set might look like:
10 3 6 8 2 1 9 0
g y h q a n b c .
This is the creative part of the homework. It asks you to
implement Insertion Sort, another algorithm for sorting,
which I briefly described in class today. A high-level
description of the algorithm is given below. The main program
should be identical to the one used in the second program
sort2.cpp, meaning that you will implement the algorithm
as a template and test it on both integers and characters,
entered in the format specified above for sort2.cpp.
You will develop the program in stages, by stepwise refinement,
first on paper, as pseudo-code, then implement it. The
pseudo-code development should roughly follow the same structure
as the one I gave you for Selection Sort, starting with a general
description in English, then refining it to include functions. All
parameters to the functions should be clearly identified as IN,
OUT or IN/OUT. The handwritten notes should be legible:
hard to read submissions will be downgraded.
And now, here's the algorithm, described at a very high level and
for integers only. If
you have difficulty with understanding it, I suggest that you:
- execute it by hand on a small example to see how it should
- email me your questions.
- come prepared to ask questions on Tuesday (the homework is due on Wednesday).
Input: an array A of integers, of size n
Output: in the same array A, the numbers in increasing
Algorithm: works in n-1 iterations. Before iteration i
(i from 2 to n-1), the first i-1
elements of the array have been
looked at and arranged in increasing order. At iteration i, the
ith element of the array is inserted in the already sorted
array of i-1 integers preceeding it. For this, the
algorithm first searches the array from 1 through
i-1 to find where the ith element would fit, then
shifts to the right those elements larger than it to make room
for its insertion in the array.
In the stepwise refinement of your program, you should have
separate functions for searching and for shifting,
invoked when inserting the current element.