Skip to main content

Insertion Sort using Python | Data Structure and Algorithm

 Code:

Note:- Scroll horizontally to see the full line of code.

def insertion_sort(a,n):
    # a is list to be sorted and n is the length of list
    for i in range(1,n):
        current=a[i]
        j=i-1
        while((a[j]>current) and j>=0):
            temp=a[j+1]
            a[j+1]=a[j]
            a[j]=temp
            j=j-1
        a[j+1]=current
        print("Iteration",i,": ",a)
    return a

Comments