def insertion_sort(lst):
for i in range(1, len(lst)):
j = i
while (j > 0) and lst[j-1] > lst[j]:
lst[j-1], lst[j] = lst[j], lst[j-1] #swap the elements
j -=1
#sort a list
L = [5, 2, 9, 3, 6, 1, 0, 7, 4, 8]
insertion_sort(L)
print("The sorted list is: ", L)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter