This tutorial was built using Python 3.6
In this tutorial we are going to be taking a look at the insertion sorting algorithm and how it works as well as how you can implement this algorithm in the Python programming language.
So the insertion sorting algorithm is a well known sorting algorithm that can sort an unsorted array in a worst case time of
O(N^2) time. It works by iterating through an array and sorting elements in a linear fashion.
- The algorithm starts at element
0in an array and considers element sorted.
- It then looks at the first element to the right of our sorted array that just contains our
- It then inserts this unsorted element in to it’s correct location.
- Our sorted array now has 2 elements
- The algorithm proceed to insert the next element into the correct position in our sorted array until the entire list has been sorted.
Implementing it in Python
Implementing it in Python can be done like so:
When executed this should give you the following output:
If you found this tutorial useful or require further assistance then please let me know in the comments section below!