A kind reader pointed out that I frequently mention algorithms, but that I haven’t defined them in the basics posts. To me, they’re so fundamental to the stuff I do for a living that I completely forgot that they’re an interesting idea.
It’s really pretty simple: an algorithm is description of a mechanical procedure for performing a mathematical task. But that’s actually a fairly sophisticated idea – the general concept of things that describe a procedure, and which can be discussed, described, and reasoned about as entities in their own right is something quite different from nearly anything that came before it.
The term algorithm has an interesting and humorous origin. Most people believe that it’s named after a Persian mathematician named “Al-Khwarizmi”. That’s actually not quite true. While the term is derived from Al-Khwarizmi’s name, it’s actually an accident. In fact, what happened was that Al-Khwarizmi wrote a book about how to perform calculations using the Hindi numbering system (which is what we know as arabic numerals). Several hundred years later, the text was translated into Latin, and in the process of transliterating the name “Al-Khwarizmi” into Latin, the title of the book became “Algorithmi on Indian Numbers”. “Algorithmi” looks like a latin plural noun – and so many European mathematicians interpreted the title of the book not as “Al-Khwarizmi talking about mathematical procedures using Indian numbers”, but “Mathematical Procedures using Indian Numbers”. And the term stuck.
So – an algorithm is a description mathematical procedure – and it’s possible to reason about an algorithm as an entity itself. Let’s look at a simple example, just to get the sense of it.
Suppose I have a list of N numbers in random order. I want to get the list into
increasing order – smallest first, largest last, ever number smaller than the number that comes after it.
One of the simplest algorithms for this is called bubble sort:
Given: a list L1...LN of N numbers in random order. Produce: a list of those N numbers in increasing order Do: repeat the following until the list no longer changes: look at each pair of adjacent numbers Li and Li+1: if Li > Li+1 then swap Li and Li+1
This will produce a sorted list. Let’s run through a quick example: the list [3,5,2,1,4].
- First pass through the list:
- Compare 3 and 5. 3<5 so move on to the next pair.
- Compare 5 and 2. 5>2, so swap. List becomes [3,2,5,1,4]
- Compare 5 and 1. 5>1, so swap. List becomes [3,2,1,5,4]
- Compare 5 and 4. 5>4, so swap. List becomes [3,2,1,4,5]
- The list changed, so we take another pass. Second pass:
- Compare 3 and 2. 3>2, so swap. List becomes [2,3,1,4,5]
- Compare 3 and 1.3>1, so swap. List becomes [2,1,3,4,5]
- Compare 3 and 4. 3<4, so move on.
- Compare 4 and 5. 4<5, so move on.
- The list changed, so we do another pass. Third pass:
- Compare 2 and 1. 2>1, so swap. List is now: [1,2,3,4,5].
- Next four comparisons are (2,3), (3,4), (4,5), none of which cause swaps.
- The list changed, so another pass. Fourth pass, everything is in order – each pair compares without causing changes. So the result is [1,2,3,4,5].
What can we say about this algorithm? Well, for one thing, it’s slow. Given a list of N elements, we might need to do up to N passes over the list, each of which will do N-1 comparisons, each of which might cause a swap. So in the worst case, this will require N2-N comparisons and swaps to get the list into order. Our example wasn’t even a worse case, and it ended up needed to do 16 comparisons (4 passes, 4 comparisons per pass), and 6 swaps. In the best case – the list is already in order – this algorithm will take one pass – N-1 comparisons, 0 swaps.
There are even some tricks using Kolmogorov-Chaitin information theory where we can show that on average, Bubble-sort will tend more towards the worse end of its possible performance than the best – but that’s beyond the scope of a basics post, except to point out that we can apply interesting mathematical analyses to the algorithm itself to figure out what it’s average performance should be, even without running it.