Python Program to Implement Insertion Sorting

01. Example:

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


list01 = input("Enter the list of numbers: ").split()
list01 = [int(x) for x in list01]
insertionSorting(list01)

print("nSorted list: ", end="")
print(list01)

Output:

Enter the list of numbers: 320 420 140 500 185 60 200 80 10 100
Sorted list: [10, 60, 80, 100, 140, 185, 200, 320, 420, 500]

 

Leave a comment