Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Python Program to perform insertion sort

To write a Python Program to perform insertion sort

Python
def insertion_sort(alist):
    for i in range(1, len(alist)):
        temp = alist[i]
        j = i - 1
        while (j >= 0 and temp < alist[j]):
            alist[j + 1] = alist[j]
            j = j - 1
        alist[j + 1] = temp

alist = input('Enter the list of numbers: ').split()
alist = [int(x) for x in alist]
insertion_sort(alist)
print('Sorted list:', end=" ")
print(alist)

OUTPUT:
Enter the list of numbers: 1 4 5 3 6 7 8

Sorted list: [1, 3, 4, 5, 6, 7, 8]