Insertion sort

Insertion sort is a simple sorting algorithm that is asymptotically equivalent to bubble sort, but is much faster in practice, because the number of swaps is at most linear. Its complexity is $$O(n^2)$$, in-place, and is stable.

Insertion sort is preferable when the number of items is small - the constant is small enough that the benefit due to excellent locality and noticably better when the entire list fits in the lower levels of caching. Insertion sort is also quick when the list is nearly sorted. Introsort exploits these properties by using this sort after quicksort or heapsort have made a few passes on the list.

Insertion sort in an array with gaps can achieve the theoretical lower bound of $$O(n \log n)$$ of sorting with high probability. (Bender, Farach-Colton, Mosteiro, 2004)

Algorithm

 * 1) At each iteration:
 * 2) Find the smallest item.
 * 3) Move the item to the front of the unsorted list.

Pseudo-code
for i from 1 to N  min = i   for j from i + 1 to N      if a[j] < a[min] min = j  swap( a[i], a[min] )
 * a is an array