Skip to content
Code it · Write it

Write insertion sort

Write insertion_sort(arr) (Python) or insertionSort(arr) (JavaScript/TypeScript). It sorts arr in place, smallest first.

Keep the left part of the array sorted. Take the next element as the key, shift every bigger element in the sorted part one step right, then drop the key into the gap.

def insertion_sort(arr):
    # Sort arr in place, smallest first.
    # Slide each element left past the bigger ones before it.
    pass