Lecture 7
- For reference, Sorting
algorithms, applets from Dominique's web page. Compare
Insertion and Selection Sort. Also look at the
applets available from the
class web page.
Next time
- 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 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 (a model will be posted
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.