My previous posts dealt with some Project Euler problems and their algorithmic solutions.
So far I was real fuzzy when it came to talking about the performance of these algorithms. So I decided to do something about it.
I picked “The Pragmatic Programmer” from my book shelf and read through chapter 6/32 “Algorithm Speed”, roughly seven pages about the basics of estimating algorithms, the O() notation, common sense estimation and algorithm speed in practice.
This chapter also includes a chart, that shows the O() notations for various algorithms like traveling salesman or selection sort.
Down the rabbit hole I go….
I decided to do some reading and writing about sorting algorithms, starting with insertion sort.
Here is a pseudocode representation of insertion sort on a zero-based list:
The algorithm consists of an outer for-loop and an inner while-loop.
The for-loop iterates over all elements in the list to be sorted starting with the second element.
Why starting with second element and not the first one? Because the assumption is that the first list element is already sorted by default.
The inner-while loop does the actual comparison of list elements and swapping if necessary.
Let’s take a zero-based list A of six integers. length(A) = 6:
The algorithm’s for-loop iterates over the list elements from A to A.
length(A – 1) = 5:
The current position is stored in the variable j.
Now, while the value of j is greater than 0 AND the value of A[j-1] is greater than the value of A[j], the elements A[j-1] and A[j] get swapped and the value of j is decreased by 1.
This is the algorithm’s first iteration:
i = 1;
j = 1;
j > 0 → true;
A[j-1] = A[1-1] = A = 5;
A[j] = A = 3;
A[j-1] > A[j] = 5 > 3 → true;
Therefore swap A[j] = 3 with A[j-1] = 5.
j = j – 1 = 1 – 1 = 0;
And that is list A after the first swap:As j = 0 > 0 → false, we do not enter the while-loop another time but continue with the for-loop.
i = 2;
j = 2;
j > 0 → true;
A[j-1] = A[2-1] = A = 5;
A[j] = A = 7;
A[j-1] > A[j] = 5 > 7 → false;
Again we do not enter the while-loop, but continue with the for loop.
i = 3;
j = 3;
The for-loop moves through the list from left to right, the while-loop backwards.
This is the complete sorting sequence:
Let’s implement the algorithm in Java.
A Romanian folk dance
By the way, I came across this video on Youtube, that teaches the workings of insertion sort, which is pure genius:
So far so good. But what about performance? No answer yet. And I don’t want to simply copy Wikipedia. So I will have to do some reading first.