<P> Example: The following table shows the steps for sorting the sequence (3, 7, 4, 9, 5, 2, 6, 1). In each step, the key under consideration is underlined . The key that was moved (or left in place because it was biggest yet considered) in the previous step is marked with an asterisk . </P> <P> Insertion sort is very similar to selection sort . As in selection sort, after k passes through the array, the first k elements are in sorted order . However, the fundamental difference between the two algorithms is that for selection sort these are the k smallest elements of the unsorted input, while in insertion sort they are simply the first k elements of the input . The primary advantage of insertion sort over selection sort is that selection sort must always scan all remaining elements to find the absolute smallest element in the unsorted portion of the list, while insertion sort requires only a single comparison when the k + 1th element is greater than the kth element; when this is frequently true (such as if the input array is already sorted or partially sorted), insertion sort is distinctly more efficient compared to selection sort . On average (assuming the rank of the k + 1th element rank is random), insertion sort will require comparing and shifting half of the previous k elements, meaning insertion sort will perform about half as many comparisons as selection sort on average . In the worst case for insertion sort (when the input array is reverse - sorted), insertion sort performs just as many comparisons as selection sort . However, a disadvantage of insertion sort over selection sort is that it requires more writes due to the fact that, on each iteration, inserting the k + 1th element into the sorted portion of the array requires many element swaps to shift all of the following elements, while only a single swap is required for each iteration of selection sort . In general, insertion sort will write to the array O (n) times, whereas selection sort will write only O (n) times . For this reason selection sort may be preferable in cases where writing to memory is significantly more expensive than reading, such as with EEPROM or flash memory . </P> <P> While some divide - and - conquer algorithms such as quicksort and mergesort outperform insertion sort for larger arrays, non-recursive sorting algorithms such as insertion sort or selection sort are generally faster for very small arrays (the exact size varies by environment and implementation, but is typically between seven and fifty elements). Therefore, a useful optimization in the implementation of those algorithms is a hybrid approach, using the simpler algorithm when the array has been divided to a small size . </P> <P> D.L. Shell made substantial improvements to the algorithm; the modified version is called Shell sort . The sorting algorithm compares elements separated by a distance that decreases on each pass . Shell sort has distinctly improved running times in practical work, with two simple variants requiring O (n) and O (n) running time . </P>

Best worst and average case of insertion sort