You are on page 1of 4

CO1401 - Programming / CO1457

Week 8 – Insertion Sort

Worksheet Key
A diamond bullet point is an action or a task. Other bullets are information:
 A task to perform
- A point of information

Important In-depth detail Advanced optional task

Always refer to the lecture notes for examples and guidance.


Also look at your previous work, many exercises are similar.

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;

for (i = 1; i < arraySize; i++)


{
insert = numbers[i];
j = i;
while ((j > 0) && (numbers[j - 1] > insert))
{
numbers[j] = numbers[j - 1];
j = j - 1;
}
numbers[j] = insert;
// invoke Display here
}
}

int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
// invoke Display here

insertionSort( numbers, SIZE );

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;
}

void BubbleSort(int a[])


{
for (int i = SIZE; i > 1; i--)
{
for (int j = 1; j < i; j++)
{
if (a[j-1] > a[j]) mySwap(a, j-1, j);
}
}
}

 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

Loop until we reach the end of the array:


3 is the next value chosen to be inserted. 3 “bubbles” through the array and
gets swapped with 9.
36 971
9 is the next value chosen to be inserted. It won’t “bubble” through the array
because it is larger than 6.
369 71
7 is the next value chosen to be inserted. 7 “bubbles” through the array and
gets swapped with 9. It won’t “bubble” through the array because it is larger
than 6.
3679 1
1 is the next value chosen to be inserted. 1 “bubbles” through the array and
gets swapped with 9, and then with 7 and so on until it reaches the end of the
array.
136797
 Essentially you are using the “bubble” mechanism to do an insert. It actually is
reasonably efficient because “bubble sort” works well with sorted data.
 Implement this version of the insertion sort.

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.

You might also like