Insertion Sort

Sort an array using the insertion sort algorithm by building a sorted portion one element at a time.

Code

Algorithms
a = arr.copy()
for i in range(1, len(a)):
    key = a[i]
    j = i - 1
    while j >= 0 and a[j] > key:
        a[j + 1] = a[j]
        j -= 1
    a[j + 1] = key
return a

Parameters

Array to sort

Server

More Python Snippets