DEV Community

SaiPavan Seelamsetty
SaiPavan Seelamsetty

Posted on

Insertion Sort - Python

Image description

def insertionSort(array):
    # Write your code here.
    for i in range(1,len(array)):
        j=i
        while j>0 and array[j]<array[j-1]:
            swap(j,j-1,array)
            j-=1
    return array        
def swap(i,j,array):
    array[i],array[j]=array[j],array[i]

#TC=O(n^2)  SC = O(1)  - Saipavan Seelamsetty
Enter fullscreen mode Exit fullscreen mode

Top comments (0)