Insertion Sort

Insertion sort is a simple sorting algorithm that works by moving the next item into its correct position in a sorted subarray seen to the left.

It simply takes the next item and swaps it backwards until it is sorted into the subarray. This makes insertion sort useful for mostly sorted data as it is adaptive due to only needing to move each element as far as necessary.

Insertion sort is a slow sorting algorithm due to needing up to n swaps to move each n elements into the sorted subarray. Thus the time complexity is O(n^2).

The red line represents the partition between the sorted subarray and the rest of the unsorted elements.

Settings