Insertion Sort is a simple sorting algorithm that builds the sorted portion of the array one element at a time. At each step, it takes the next element and inserts it into its correct position among the elements that are already sorted.
A common real-world analogy is sorting playing cards in your hand: you pick one tl-card at a time and place it into the correct position among the cards already arranged.
Insertion Sort maintains two parts of the array:
For each new element, the algorithm shifts larger elements of the sorted portion one position to the right and inserts the element into the gap created.
| Case | Time | Space | Stable? |
|---|---|---|---|
| Best | O(n) | O(1) | Yes |
| Average | O(n^2) | O(1) | Yes |
| Worst | O(n^2) | O(1) | Yes |
The best case occurs when the array is already sorted, because each element is checked once and no shifting is needed. The worst case occurs when the array is reverse sorted, because each new key must be moved all the way to the front.
Insertion Sort performs very well when the array is already sorted or almost sorted. In such cases, each element only needs a small number of comparisons and shifts, so the total running time becomes close to O(n).
This is why insertion sort is often used inside more advanced sorting algorithms for small or nearly sorted subarrays.
Consider the array [12, 11, 13, 5, 6].
[11, 12, 13, 5, 6][11, 12, 13, 5, 6][5, 11, 12, 13, 6][5, 6, 11, 12, 13]import java.util.Arrays;
public class InsertionSort {
static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Shift larger elements to the right.
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
static void insertionSortRec(int[] arr, int n) {
if (n <= 1) return;
insertionSortRec(arr, n - 1);
int key = arr[n - 1];
int j = n - 2;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6};
System.out.println("Before: " + Arrays.toString(arr));
insertionSort(arr);
System.out.println("After: " + Arrays.toString(arr));
int[] sorted = {1, 2, 3, 4, 5};
insertionSort(sorted);
System.out.println("Sorted: " + Arrays.toString(sorted));
}
}
For the array [12, 11, 13, 5, 6]:
| i | Key | Sorted Portion Before | Array After Insertion |
|---|---|---|---|
| 1 | 11 | [12] | [11, 12, 13, 5, 6] |
| 2 | 13 | [11, 12] | [11, 12, 13, 5, 6] |
| 3 | 5 | [11, 12, 13] | [5, 11, 12, 13, 6] |
| 4 | 6 | [5, 11, 12, 13] | [5, 6, 11, 12, 13] |
Insertion Sort is stable because equal elements are not moved past one another unnecessarily. If two values are equal, their original relative order is preserved.
This matters when sorting records by multiple fields.
Insertion Sort uses only a few extra variables such as the key and index counters. It does not need an extra array, so its space complexity is O(1).
| Feature | Insertion Sort | Bubble Sort | Selection Sort |
|---|---|---|---|
| Best case | O(n) | O(n) | O(n^2) |
| Average / Worst | O(n^2) | O(n^2) | O(n^2) |
| Stable | Yes | Yes | No |
| Good for nearly sorted data | Yes | Sometimes | No |
| Main strength | Fast for small and nearly sorted arrays | Simple concept | Minimum number of swaps |
Avoid insertion sort for large random datasets because its average and worst-case times are quadratic.
Explore 500+ free tutorials across 20+ languages and frameworks.