
CPSC 5115 Algorithm Analysis and Design
Program 1 Due Wednesday, September 29, 2004
General Policies
1. Programs must be written in an approved programming language.
Languages explicitly approved: Visual Basic, C++, C#, Java
Languages explicitly not approved: COBOL, FORTRN, LISP, Assembler
2. Programs must be written in the style of a C++ console application. Specifically, I
do not want to see any window handling routines or message loops.
3. Programs must be written with readability in mind. I am a great advocate of
structured programming, with principles such as
a) Use many short, well-defined, functions.
b) Watching page breaks in the listing to facilitate code readability.
c) Avoidance of clever coding tricks.
d) Liberal use of parentheses to avoid reliance on complicated operator
precedence hierarchies. I don’t want to check a C++ manual.
e) In C++, writing equality comparisons as (const == variable, say 0 == x)
Avoid constructs such as if (x = 0) even when it makes sense.
f) Every program has header comments identifying the program, the class for
which it is written (CPSC 5115), and the name of the author.
4. Programs must take input from a text file and write output to another text file.
The Assignment
As this assignment consists only of copying code from the textbook or some other source,
you are assigned two programs.
1) Implement merge-sort, either as in the book or with the modification to sort
directly any array of two elements.
2) Implement quicksort, with any valid partition algorithm. The textbook uses the
simplest method for selecting a pivot element. There are other methods that can be
used. You may modify the book’s version of quicksort as follows.
a) Direct sorting of any array of two elements.
b) Select the pivot of A[L .. R] as follows
Let M = (L + R) / 2
Let X1 = A[L], X2 = A[M], and X3 = A[R].
Use the middle value of X1, X2, and X3 as the pivot.
c) You may use a quicksort from any other source, provided only
that you acknowledge the source in your comments.
NOTE:When I indicate that “you may” apply a modification to an algorithm, this
implicitly states that you are free not to apply the modification.