CS 112
Lecture 8
Lab: Insertion Sort, with templates
Insertion Sort
- Description
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 by scanning the
array from left to right. At iteration i,
the ith element is considered and inserted in the part of the array
that has already been sorted (from 1 to i-1).
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.
- Developing the program by stepwise refinement.
We 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.
In the stepwise refinement of your program for insertion sort,
you should have
separate functions for searching and for shifting,
invoked when inserting the current element.
The main program
should be similar to the one I designed in class for Selection
Sort:
reads the input, sorts, then prints the result.
- Insertion sort with simple templates:
modify the program we just wrote for
insertion sort on integer arrays (the incomplete model started in
class is now posted
here and
here
), to sort both an array of
characters and an array of integers. Use templates.
- Insertion sort with templates and functions passed as parameters:
modify the templated program we just wrote for
insertion sort to sort, in addition, an array of
CustRec's by id and by balance.
New C++ topic:
Object-oriented programming in C++
- Classes: introduction to object-oriented programming.
- A class is similar to a struct but with extra stuff added to
it: functions that work only on objects of that type.
- Object-oriented programming: induces a certain way of
thinking, whereas programs manipulate objects using only
the methods (or functions) that are allowed to be
applied on those objects.
- Defining classes in C++. An example.
Cookie Jar abstract data type.
- Header files.
- Compiling complex programs with many source files using Makefiles.
- Lab: copy, test and understand the Cookie Jar example. Then modify
the main program by adding a menu-based user interface, which allows the
user to select the class function to be tested next.
Summary of C++ and Unix basics for working with classes
- Structure of
a C++ program using classes (header file, implementation file,
makefile, testdriver).
- Wisely using header files. Example: the
bool.h header file in the stack class example.
- Makefiles:
what they are and how to use them.
- Compiling a class implementation without creating an executable file.
- Creating an executable file from previously compiled object files.
- Compiling only the files that have been modified, or which
depend on other files that have been modified.
- Makefiles. An example of a good
Makefile for the hwk stack exercise.
- Tutorial on the
make utility.
- Problems when compiling on both
grendel and hermite. Read this carefully!!!
Ileana Streinu