11.4 Insertion sort¶
Insertion sort is a simple sorting algorithm that works much like arranging a hand of playing cards.
Choose a base element from the unsorted section, compare it with the elements in the sorted section to its left, and insert it into the correct position.
Figure 11-6 shows how to insert an element into an array. Let the base element be base. Shift all elements between the target index and base one position to the right, then assign base to the target index.

Figure 11-6 A single insertion operation
11.4.1 Algorithm steps¶
The overall insertion sort process is shown below.
- Initially, the first element of the array is treated as sorted.
- Select the second element as
baseand insert it into the correct position. The first two elements are now sorted. - Select the third element as
baseand insert it into the correct position. The first three elements are now sorted. - Continue in this way. In the final round, select the last element as
baseand insert it into the correct position. All elements are now sorted.

Figure 11-7 The insertion sort process
An implementation follows:
def insertion_sort(nums: list[int]):
"""Insertion sort"""
# Outer loop: the sorted range is [0, i-1]
for i in range(1, len(nums)):
base = nums[i]
j = i - 1
# Inner loop: insert base at the correct position in the sorted range [0, i-1]
while j >= 0 and nums[j] > base:
nums[j + 1] = nums[j] # Shift nums[j] one position to the right
j -= 1
nums[j + 1] = base # Place base at the correct position
/* Insertion sort */
void insertionSort(vector<int> &nums) {
// Outer loop: the sorted range is [0, i-1]
for (int i = 1; i < nums.size(); i++) {
int base = nums[i], j = i - 1;
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // Shift nums[j] one position to the right
j--;
}
nums[j + 1] = base; // Place base at the correct position
}
}
/* Insertion sort */
void insertionSort(int[] nums) {
// Outer loop: the sorted range is [0, i-1]
for (int i = 1; i < nums.length; i++) {
int base = nums[i], j = i - 1;
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // Shift nums[j] one position to the right
j--;
}
nums[j + 1] = base; // Place base at the correct position
}
}
/* Insertion sort */
void InsertionSort(int[] nums) {
// Outer loop: the sorted range is [0, i-1]
for (int i = 1; i < nums.Length; i++) {
int bas = nums[i], j = i - 1;
// Inner loop: insert bas at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > bas) {
nums[j + 1] = nums[j]; // Shift nums[j] one position to the right
j--;
}
nums[j + 1] = bas; // Place bas at the correct position
}
}
/* Insertion sort */
func insertionSort(nums []int) {
// Outer loop: the sorted range is [0, i-1]
for i := 1; i < len(nums); i++ {
base := nums[i]
j := i - 1
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
for j >= 0 && nums[j] > base {
nums[j+1] = nums[j] // Shift nums[j] one position to the right
j--
}
nums[j+1] = base // Place base at the correct position
}
}
/* Insertion sort */
func insertionSort(nums: inout [Int]) {
// Outer loop: the sorted range is [0, i-1]
for i in nums.indices.dropFirst() {
let base = nums[i]
var j = i - 1
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while j >= 0, nums[j] > base {
nums[j + 1] = nums[j] // Shift nums[j] one position to the right
j -= 1
}
nums[j + 1] = base // Place base at the correct position
}
}
/* Insertion sort */
function insertionSort(nums) {
// Outer loop: the sorted range is [0, i-1]
for (let i = 1; i < nums.length; i++) {
let base = nums[i],
j = i - 1;
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // Shift nums[j] one position to the right
j--;
}
nums[j + 1] = base; // Place base at the correct position
}
}
/* Insertion sort */
function insertionSort(nums: number[]): void {
// Outer loop: the sorted range is [0, i-1]
for (let i = 1; i < nums.length; i++) {
const base = nums[i];
let j = i - 1;
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // Shift nums[j] one position to the right
j--;
}
nums[j + 1] = base; // Place base at the correct position
}
}
/* Insertion sort */
void insertionSort(List<int> nums) {
// Outer loop: the sorted range is [0, i-1]
for (int i = 1; i < nums.length; i++) {
int base = nums[i], j = i - 1;
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // Shift nums[j] one position to the right
j--;
}
nums[j + 1] = base; // Place base at the correct position
}
}
/* Insertion sort */
fn insertion_sort(nums: &mut [i32]) {
// Outer loop: the sorted range is [0, i-1]
for i in 1..nums.len() {
let (base, mut j) = (nums[i], (i - 1) as i32);
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while j >= 0 && nums[j as usize] > base {
nums[(j + 1) as usize] = nums[j as usize]; // Shift nums[j] one position to the right
j -= 1;
}
nums[(j + 1) as usize] = base; // Place base at the correct position
}
}
/* Insertion sort */
void insertionSort(int nums[], int size) {
// Outer loop: the sorted range is [0, i-1]
for (int i = 1; i < size; i++) {
int base = nums[i], j = i - 1;
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
// Shift nums[j] one position to the right
nums[j + 1] = nums[j];
j--;
}
// Place base at the correct position
nums[j + 1] = base;
}
}
/* Insertion sort */
fun insertionSort(nums: IntArray) {
//Outer loop: the sorted prefix grows to 1, 2, ..., n elements
for (i in nums.indices) {
val base = nums[i]
var j = i - 1
// Inner loop: insert base at the correct position in the sorted range [0, i-1]
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j] // Shift nums[j] one position to the right
j--
}
nums[j + 1] = base // Place base at the correct position
}
}
### Insertion sort ###
def insertion_sort(nums)
n = nums.length
# Outer loop: the sorted range is [0, i-1]
for i in 1...n
base = nums[i]
j = i - 1
# Inner loop: insert base at the correct position in the sorted range [0, i-1]
while j >= 0 && nums[j] > base
nums[j + 1] = nums[j] # Shift nums[j] one position to the right
j -= 1
end
nums[j + 1] = base # Place base at the correct position
end
end
Visualize execution
11.4.2 Algorithm properties¶
- Time complexity: \(O(n^2)\); adaptive. In the worst case, the insertion operations require \(n - 1\), \(n-2\), \(\dots\), \(2\), and \(1\) iterations, totaling \((n - 1) n / 2\), so the time complexity is \(O(n^2)\). For ordered data, an insertion can stop early. A fully sorted input achieves the best-case time complexity of \(O(n)\).
- Space complexity: \(O(1)\); in-place. Pointers \(i\) and \(j\) use constant extra space.
- Stable. Each element is inserted to the right of equal elements, preserving their order.
11.4.3 Why use insertion sort?¶
Insertion sort takes \(O(n^2)\) time, while quick sort, which we will study shortly, takes \(O(n \log n)\). Despite its higher time complexity, insertion sort is usually faster for small datasets.
This resembles the comparison between linear and binary search. Algorithms such as quick sort, with \(O(n \log n)\) time complexity, use divide and conquer and tend to involve more elementary operations. When the dataset is small, \(n^2\) and \(n \log n\) are relatively close, so asymptotic complexity is not the dominant factor; the number of elementary operations per round matters more.
In fact, the built-in sorting functions of many programming languages, such as Java, use insertion sort. The general approach is to use a divide-and-conquer algorithm such as quick sort for long arrays and insertion sort directly for short ones.
Although bubble sort, selection sort, and insertion sort all take \(O(n^2)\) time, insertion sort is used much more often in practice, mainly for the following reasons.
- Bubble sort swaps elements, using a temporary variable and three elementary operations. Insertion sort shifts elements by assignment, requiring just one operation. Bubble sort therefore usually has higher computational overhead.
- Selection sort takes \(O(n^2)\) time in every case. For partially sorted data, insertion sort is usually more efficient than selection sort.
- Selection sort is not stable and therefore cannot be used for multilevel sorting.