CS 112b
Spring 2001
Ileana Streinu
Homework 2
Thursday Feb. 8, 2001
Program due Wednesday 2/14/2000
by midnight,
pseudo-code to be handed in on Tuesday 2/13/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 Feb. 13, 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 (allocated) array(s) used for this program should have a
size of at least 25 elements, but you should test it on arrays of
various sized (as entered by the user).
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 on Tuesday, 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:
The algorithm works in n-1 iterations. At each iteration,
a new element is considered and inserted in a part of the array
that has already been sorted. More precisely, in the beginning
(iteration 1), we look at the array consisting of the first
element: it is already sorted, of course. In general, right after
iteration i, the array consisting of the first i
elements is in sorted order.
Now at each 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. Obviously, I will not
consider solutions which simply copy the code from the textbook
(they do not use separate functions for shift and insert).
The program
- If you do a program that only reads once an array of integers,
sorts it with Insertion Sort and prints the result, you've got a
(VERY GOOD) grade of A-. For a full A, I expect a little more
polishing of the program. The main function should contain a
loop, waiting for the user to enter a new array of integers (one
per line, ending with a 0), which is sorted and printed. When the
user types in -1, the main loop ends. Assume that all the numbers
to be sorted are strictly positive integers.
- Extra credit: write a separate function to sort (using
Insertion Sort) an array of characters. Use the same type of user
interface for testing this function. An array of characters, as
entered by the user, should contain characters different from
the dot '.', and should end when a dot is encountered.
To submit
Basic program:
A-level program:
- sortA.cpp
- typescriptA
- dataA
Extra credit program:
- sortE.cpp
- typescriptE
- dataE
Ileana Streinu