Skip to the content.

Insertion Sort

Insertion sort is similar to how we sort the playing cards in a hand of a bridge game. The process of sorting a hand is as follows:

When sorting an array of elements, we assume that the elements are divided into two parts, viz., sorted part and unsorted part. The unsorted elements occur to the right of sorted elements. Initially, only the first element is considered sorted. The process is as follows:

The figure below illustrates the insertion sort algorithm.

The critical point in insertion sort is data movement. If there is no data movement, then the list is sorted. A pair of loops control it. The first loop picks the next unsorted element from the list. The second loop controls the data movement, comparing the picked element with each sorted element one by one, beginning from the right.

Since we have to pick one element at a time from the left of the unsorted part, the first loop runs n-1 times. The second loop scans all elements of the sorted part to create a slot where the next unsorted element can be slipped. Since, worst case size of sorted elements is n-1, the second loop may run for n-1 times. Therefore, the worst-case running time of insertion sort is O(n2).

The algorithm is as follows:

procedure insertionSort(int a[n]) {
  int  i, j, n, gap;

  for(i = 1; i < n; i++) {
        gap = a[i]; // Save the next unsorted element
        j = i-1;    // Compare and shift starting with rightmost sorted element
        while(j >= 0 && a[j] > gap) {
            a[j+1] = a[j]; // Shift elements to right to create slot 
            j--;
        }
        a[j+1] = gap; // Slot for new element
  } 
 
  return;
}

C Program for Insertion Sort

Back to Index