My first encounter on my journey to re-learn algorithm is the insertion sort algorithm. This the code that I took from TADM book:
And this is the illustration that I took from the net:
From the code and the illustration, you can see the characteristics of insertion sort are:
1. Start from index 1. Why? Because if the list length is 1 then there is no point to sort it.
2. There are 2 loops. One for iterating the list and the other for comparing the current index with the item on the behind. The second loop only keeps iterating if the current item is lower than the item behind.
3. Swapping. When comparing backward, the lower item will be swapped with the higher item. So this swapping needs an array to be fast.