Insertion Sort
Abstract
Many applications require that data be ordered and maintained. Insertion sort is one sorting algorithm that puts data in order. Some examples of humans manually using insertion sort include sorting a deck of cards, or sorting books on a bookshelf.
The Insertion Sort
One common sorting approach is based on code that assumes that the first part of an array is ordered and then adds successive items to this array segment until the entire array is sorted. To understand this approach, we first consider how to add one item to an ordered array segment. We then apply this work to each array element in turn to yield an ordered array.
Maintaining An Ordered Array Segment
Suppose items A[0], ..., A[k-1] are ordered in array A:
The following code inserts an item into the array, so that items A[0], ..., A[k] become ordered:
int i = k-1;
while ((i >= 0) && a[i] > item){
a[i+1] = a[i];
i--;
}
a[i+1] = item;
Using this basic insertion step, an array A can be sorted iteratively according to the following outline:
- Consider A[0] as a sorted initial segment of array A. (That is, in the example, consider k = 0.)
- Insert A[1] into segment A[0] to get ordered segment A[0], A[1].
- Insert A[2] into segment A[0], A[1] to get ordered segment A[0], A[1], A[2].
- Insert A[3] into segment A[0], ..., A[2] to get ordered segment A[0], ..., A[3].
- Insert A[4] into segment A[0], ..., A[3] to get ordered segment A[0], ..., A[4].
- Et cetera.
This outline gives rise the the following code, called an insertion sort.
void insertionSort (int [] a) {
// method to sort using the insertion sort
for (int k = 1; k < length; k++) {
int item = a[k];
int i = k-1;
while ((i >= 0) && a[i] > item){
a[i+1] = a[i];
i--;
}
a[i+1] = item;
}
}
It is important to notice that the item being compared is not switching place with each item it compares to. The items that are compared against are shifted; the element being compared is inserted.
Insertion Sort Characteristics
This sorting algorithm is relatively simple, efficient for small data sets and data sets that are already partially sorted, stable in the sense that it does not switch identical elements, and only requires a constant amount of additional memory space (that is, the amount of memory for the set of elements, plus memory for one element of the set). The amount of time this algorithm takes to sort a set is dependent upon the set itself. For a set with n elements, if the set is sorted (or nearly so), the algorithm takes only a single time unit for each element, so it takes n time units. On the other hand, if the set is in reverse order (that is, largest to smallest), the time approaches n-squared. You will see the previous two statements proven in a later course.
Example
Image ExampleImage borrowed from Wikipedia.org under a Creative Commons license.