Home » Python program to sort a sequence using insertion sort

Python program to sort a sequence using insertion sort

Python program a sort a sequence using insertion sort

lst=list(eval(input("Enter list elements:")))
print("Original list is",lst)
for i in range(1,len(lst)):
    key=lst[i]
    j=i-1
    while j>=0 and key<lst[j]:
        lst[j+1]=lst[j]
        j=j-1
    else:
        lst[j+1]=key
print("List after sorting:",lst)

Contents

Output

Enter list elements:56,86,54,76,58,87

Original list is [56, 86, 54, 76, 58, 87]

List after sorting: [54, 56, 58, 76, 86, 87]