Insertion Sort on A Glance

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:

Insertion sort illustration

Insertion Sort


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.



This entry was posted in Uncategorized. Bookmark the permalink.
Add Comment Register

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>