Data Structures and Algorithms: Insertion Sort
The Insertion Sort Algorithm


Insertion sort algorithm visual example
Implementing Insertion Sort Algorithm
Insertion sort algorithm is simple to implement, if we want to write an insertion sort algorithm we will need to use a for-loop inside the main function which we will be using it to iterate over all of the elements in the list and a while-loop inside the helper function which will be doing the most of the work. Let's see an example of an insertion sort algorithm in Python.
#insertion sort function
def insertionSort(a):
for i in range(1, len(a)):
moveLeft(a, i)
#helper function that moves a[j]
def moveLeft(a, j):
x = a.pop(j)
while j > 0 and a[j - 1] > x:
j -= 1
a.insert(j, x)
Insertion sort imlementation in Python
At the beginning of the example, we are defining a function insertionSort
with one argument (a
) which is the list. Inside the function, we have a for-loop that iterates through every element from the 1 (which is the second element, because the first is already sorted) to the end at the size of the list. Inside we have a helper function called moveLeft
where we pass the reference to the list as the first argument a
and the current index j
. Removing the item at a specified location is straightforward, we simply need to call a method named pop defined in the list class, and it will remove the object at location j
and saves it in x
. On the next line, we have a while-loop that implements our leftward scan. Because j
is the location where x
was used to be, the first time the program executes the while-loop it will compare x
to the item that was to its left. If the element on the left is larger it will need to go further to the left. The line with the j -= 1
in the while-loop subtracts one from j. On the last line, we simply insert the x
at index j
into the list a
.
Analysis of Insertion Sort Algorithm
The best case input about the insertion sort algorithm is when the list is already sorted, which has a linear running time (i.e., O(n)). The worst-case input is when the list is sorted in reverse order, this gives us a quadratic running time (i.e., O(n2)). The average case is also quadratic, which puts insertion sort not very useful for sorting large lists. However, insertion sort is one of the fastest algorithms for sorting very small lists. For a given list with n size of items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed. The worst case is when the value is not in the list or occurs only once at the end of the list, in which case n comparisons are needed.
If you want to learn more in-depth about algorithms and data structures, I highly recommend you to check this book: Introduction to Algorithms, 3rd Edition (The MIT Press)