To write a python program Insertion sort. code example
Example 1: insertion sort in python
def insertion(s):
for i in range(0,len(s)-1):
if s[i]>s[i+1]:
s[i],s[i+1]=s[i+1],s[i]
for j in range(i,0,-1):
if s[j]<s[j-1]:
s[j],s[j-1]=s[j-1],s[j]
print(s)
insertion([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]
## swapping
i = i -1
return lst
print(insertion_sort([4,2,2,3,4,3,1,1,1,2]))