Runtime: O(N^2), where N is the number of list elements. Insertion sort is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort, which run in O(N*log(N)).
Algorithm Steps:
For each element, moving from left to right:
Move the element into the sorted subarray. One way to do this is by swapping side-by-side elements repeatedly.
Slide 0 of 20
Use Arrow keys
Python Code
definsertion_sort(lst):for i, cur inenumerate(lst): j = i -1while j >=0and lst[j]> cur: lst[j], lst[j +1]= lst[j +1], lst[j]# alternatively, use temporary var j -=1 lst[j +1]= cur
lst =[1,8,32,-3,2,10,1,5,100]insertion_sort(lst)print("list sorted:", lst)