Aug 5, 2015
Insertion Sort algorithm explained in python with example
Aug 5, 2015 Posted by Aman Deep
Insertion sort implemented in python:
In the python code below we have a list containing 10 unsorted numbers to sort. In the first while loop we are iterating from the 2nd element so that the first element of the list becomes the first element of the sorted list. The first while loop selects an element from the unsorted list and the second while loop (nested inside first one) compares it with elements of the sorted list, if the selected element is less than the adjacent left element (sorted portion) then the left element is shifted into the current element's place and the current element is copied into the left element's place. The last while loop finally displays the sorted list.
Time complexity: O(n²)
watch this video for understanding the algorithm, its implementation and time complexity.