Files
30-seconds-of-code/snippets/insertionsort.md
2018-02-20 15:03:22 +05:30

616 B

Insertion Sort

Implementation

arr = [] # list to sort


def insertionsort(arr):

    for i in range(1, len(arr)):
        key = arr[i]
        j = i-1
        while j>=0 and key < arr[j]:
            arr[j+1] = arr[j]
            j -= 1
            arr[j+1] = key

Example

arr = [7,4,9,2,6,3]

def insertionsort(arr):

    for i in range(1, len(arr)):
        key = arr[i]
        j = i-1
        while j>=0 and key < arr[j]:
            arr[j+1] = arr[j]
            j -= 1
            arr[j+1] = key


insertionsort(arr)
print('Sorted %s'  %arr) # sorted [2, 3, 4, 6, 7, 9]