Introduction to insertion sort

In this step, you are going to have a look at another popular algorithm for sorting a list of items — insertion sort.

The insertion sort algorithm demonstrates to learners how the downfalls of bubble sort can be improved upon, it does away with the excess looping over the entire list that features so prominently in bubble sort.

An introduction to insertion sort

The insertion sort algorithm will split the list to be sorted into two sections, called sublists. These are not separate list variables, rather they are two parts of the original list — a sorted sublist and an unsorted sublist. The sorted sublist starts with just one item (the first in the list) and the rest of the list is in the unsorted sublist.

As the algorithm progresses, each item is evaluated and inserted into the correct place in the ordered sublist.

Consider the following list of playing cards. I am going to use insertion sort to put them in ascending order.