15
Insertion Sort
Some properties of insertion sort:
1) For an array of
n
elements, the array is sorted after
n
− 1 passes.
2) After the
k
th pass, a[0], a[1], ..., a[k] are sorted with respect to
each other but not necessarily in their final sorted positions.
3) The worst case for insertion sort occurs if the array is initially sorted
in reverse order, since this will lead to the maximum possible number
of comparisons and moves.
4) The best case for insertion sort occurs if the array is already sorted
in increasing order. In this case, each pass through the array will
involve just one comparison, which will indicate that “it” is in its correct
position with respect to the sorted list. Therefore, no elements will
need to be moved.
Note: For selection sort, there is no worst or best case. Finding the
smallest at each iteration requires traversing the array to the end.