Insertion Sort Visualization

How Insertion Sort Works

Insertion sort iterates through an array and grows a sorted array behind it. On each iteration, insertion sort removes one element from the unsorted list, finds the location it belongs within the sorted list, and inserts it there. It repeats until no unsorted elements remain.

function insertionSort(array):
    for i from 1 to array.length - 1:
        key = array[i]
        j = i - 1
        while j >= 0 and array[j] > key:
            array[j + 1] = array[j]
            j--
        array[j + 1] = key
        visualize(array, i, j + 1)