CS 112b
Spring 2000
Ileana Streinu
Homework 3
Thursday February 10, 2000
Program due Wednesday 2/16/2000
by midnight,
pseudo-code to be handed in on Thursday 2/17/2000, 8:00am (before class).
Outline
Your task is to write in C++ three programs implementing sorting
algorithms. The first two 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. 17, at 8:00am
(before class).
To make the task of testing and grading this homework easier for
me and the TAs, 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).
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 listed in class today, make your code readable and be
generous with comments. No late submissions will be
accepted. However, to relieve some of the pressure on the third
part of the homework, I reassure
you that, if the pseudo-code is well done but you ran into
last-minute unfixed bugs, the penalty will not be high: the
emphasis in the third part of the homework is on careful
design, not perfectly debugged program.
Details below.
sort1.cpp
This program is based on the Selection Sort handout I distributed
in class. Your task is to implement the algorithm, as described
by the pseudo-code on the last page (using separate functions for
computing the minimum and for swapping). sort1.cpp should
be designed for sorting an array of
integers. The main program contains, in addition, calls to a
function which reads the unsorted array from the user and a
function which prints the sorted array on the standard output
(terminal).
sort2.cpp
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 .
sort3.cpp
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
work.
- email me your questions.
- email me to arrange for extra office hours: I am available on
Friday all day, Saturday morning before 10 and on Tuesday afternoon.
- go to the early (Sunday) TA help session.
Insertion Sort
Input: an array A of integers, of size n
Output: in the same array A, the numbers in increasing
order.
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.
Last modified February 10, 2000.
Ileana Streinu