insertion sort python medium code example
Example 1: insertion sort python
def insertionSort(alist):
for i in range(1,len(alist)):
current = alist[i]
while i>0 and alist[i-1]>current:
alist[i] = alist[i-1]
i = i-1
alist[i] = current
return alist
print(insertionSort([5,2,1,9,0,4,6]))
Example 2: insertion sort python
def insertion_sort(lst):
for i in range(1,len(lst)):
while i > 0 and lst[i - 1] >lst[i] :
lst[i - 1], lst[i] = lst[i] , lst[i - 1]
i = i -1
return lst
print(insertion_sort([4,2,2,3,4,3,1,1,1,2]))