Professional Documents
Culture Documents
CO1401 - 1457 Week 8 Lab
CO1401 - 1457 Week 8 Lab
Worksheet Key
A diamond bullet point is an action or a task. Other bullets are information:
A task to perform
- A point of information
Programming Exercise 1
Look at the code I gave in the lecture for insertion sort. I want you to ensure
that you fully understand what it is doing.
const int SIZE = 8;
void insertionSort(int numbers[], int arraySize)
{
int i, j, insert;
int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
// invoke Display here
system( "pause" );
}
Write a function called Display which will display the contents of the integer
array being sorted.
Invoke the function at the points in the code that I’ve indicated. Study the way
that the insertion sort is working.
You did breakpoints with Laurent in week 5. See his worksheet and lecture for
this. Place a breakpoint into the “InsertionSort” function. You place a breakpoint
by clicking in the light grey margin next to your code. A red button appears when
you set a breakpoint.
Run your code. The effect of the breakpoint will be to halt execution of the
program when it hits the breakpoint. You can examine the values of the variables
in your program by “hovering” over them using the mouse. Alternatively look in
the “autos” pane. You can step through the code using the F10 key.
Examine the way in which the variables change as the sorting algorithm
operates on the array.
Programming Exercise 2
Create an array of size 6 of characters.
Initialise it is some suitable values.
Convert the InsertSort function to deal with characters and then sort your
array.
Programming Exercise 3
A “Bubble sort” is one alternative to insertion sort. It is less efficient on longer
arrays.
void mySwap( int a[], int pos1, int pos2 )
{
int tmp = a[pos1];
a[pos1] = a[pos2];
a[pos2] = tmp;
}
Examine the code given above. Copy and paste it into your code. Apply the sort
function to the “numbers” array.
Use the Display function you wrote to examine the approach that the bubble sort
algorithm uses.
Use breakpoints to look at how the variables in the code change when you run the
BubbleSort function.
Compare the bubble sort algorithm with the insertion sort algorithm.
Programming Exercise 4
I said in the lecture that it is possible to use a variety of different search
mechanisms for the “sorted” or left hand side of the array. It is possible to use
other approaches to the “slide” and “insert” approach I showed (and which the
code given in Exercise 1 uses).
Convert the InsertionSort code to use a “bubble sort” purely for the sorted part of
the array.
The algorithm would be:
Select a value to be inserted (this is the same as the code you’ve already got).
“Bubble” the value to be inserted with the “sorted” part of the array until it
reaches its correct position in the array. Note that it’ll be easier to bubble the
values so that the lowest value bubbles leftwards (to the start of the array).
For example:
63971
6 is on the left hand side of the array and so the first value chosen
6 3971
6 is on the left hand side of the array and so the first value chosen
Advanced Tasks
If you are feeling mathematical then have a look at the lecture notes for CO2402:
Advanced C++ for week 6: “Computational Complexity”. The material I have
covered about algorithms and efficiency leads directly into computational
complexity and a particular way of estimating this called “Big O” notation. Read
through the material and see if you can give the Big O efficiency of linear search,
binary search and insertion sort.
Convert the insertion sort algorithm into a class.