CS 112a
Fall 2000
Ileana Streinu
Homework 2
Thursday Sept. 14, 2000
Program due Wednesday 9/20/2000
by midnight,
pseudo-code to be handed in on Tuesday 9/19/2000, 8:00am (before class).
Outline
Your task is to write in C++ a program implementing the
Insertion Sort algorithm.
It should be named sort.cpp.
The program should contain all the necessary functions and the
main program in one file, named as indicated. You will submit
it in Hw2, together with a typescript file.
You will also have to submit
handwritten pseudo-code, which is due on Tu Sept. 19, 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).
The output should appear on a
separate line, with the values separated by spaces.
The internal array(s) used for this program 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 the program
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.
Details below.
Insertion Sort is 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 similar to the one I designed in class for Selection
Sort:
reads the input, sorts than prints the result.
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.
- 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 Sept. 13, 2000.
Ileana Streinu