CLRS Chapter: 2.1
Worst-case Complexity: O(n²)
Space Complexity: O(1)
Technical Sheet. Based on Introduction to Algorithms (Cormen et al., 4th Edition), Chapter 2 (Section 2.1: Insertion sort).
1. The Sorting Problem and the Insertion Sort Approach
The basic sorting problem consists of taking a sequence of numbers and rearranging them into a monotonically increasing order.
Input: a sequence of numbers .
Output: a permutation of the input sequence such that:
The elements to be sorted are known as keys. In computational practice, keys are often associated with additional data called satellite data. Together, the key and its satellite data form a record (for instance, a student’s data in a database ordered by their GPA).
How Does Insertion Sort Work?
Insertion Sort is an efficient algorithm for sorting a small number of elements. Its operation is identical to the way a person sorts a hand of playing cards:
- Start with an empty left hand and the cards face down on the table.
- Draw one card at a time from the table and insert it into its correct position within the left hand.
- To find the correct position, compare the new card with the ones already in the hand, from right to left.
- At all times, the cards in the left hand are kept sorted.
2. Pseudocode Specification
The INSERTION-SORT procedure takes an array and the number of elements , operating over the index range .
INSERTION-SORT(A, n)
1 for i = 2 to n
2 key = A[i]
3 // Insert A[i] into the sorted subarray A[1 .. i - 1]
4 j = i - 1
5 while j > 0 and A[j] > key
6 A[j + 1] = A[j]
7 j = j - 1
8 A[j + 1] = key
3. Proof of Correctness Using Loop Invariants
To prove that an algorithm is correct, the technique of loop invariants is used, which is a form of mathematical induction. An invariant must satisfy three properties:
- Initialization. It is true prior to the first iteration of the loop (base case).
- Maintenance. If it is true before an iteration, it remains true before the next one (inductive step).
- Termination. When the loop terminates, the invariant gives a useful property that helps show that the algorithm is correct.
Applying the Invariant to Insertion Sort
Invariant. At the start of each iteration of the for loop (lines 1–8), the subarray consisting of elements consists of the elements originally present in , but now in ascending order.
Proof.
- Initialization. When , the subarray contains only , which is the original element. By definition, a subarray of a single element is sorted. The invariant holds.
- Maintenance. The loop body moves elements one position to the right until finding the right spot for the
key(lines 4–7) and inserts it into the correct position (line 8). This extends the sorted subarray to . Incrementing for the next iteration preserves the invariant. - Termination. The loop terminates when exceeds (that is, when ). Substituting by in the invariant statement, the subarray contains all the original elements completely sorted. Therefore, the algorithm is correct.
4. C++20 Implementation
Translation of the pseudocode into C++20, adapted to the language’s zero-based indexing while preserving CLRS logic:
#include <iostream>
#include <vector>
// Insertion Sort implementation based on CLRS (Cormen)
template <typename T>
void insertionSort(std::vector<T>& A) {
const size_t n = A.size();
// Iterate from the second element (index 1) to the end
for (size_t i = 1; i < n; ++i) {
T key = A[i];
int j = static_cast<int>(i) - 1;
// Shift elements greater than the key to the right
while (j >= 0 && A[j] > key) {
A[j + 1] = A[j];
j--;
}
// Insert the key into its correct position
A[j + 1] = key;
}
}
int main() {
std::vector<int> A = {5, 2, 4, 6, 1, 3};
std::cout << "Original array: ";
for (int x : A) std::cout << x << " ";
std::cout << "\n";
insertionSort(A);
std::cout << "Sorted array: ";
for (int x : A) std::cout << x << " ";
std::cout << "\n";
return 0;
}
5. Running Time Analysis
The running time of Insertion Sort depends on the input instance. The algorithm sorts in place: it only uses a constant amount of additional memory, .
Best Case
If the array is already completely sorted, the while condition () is never met. The running time is linear in the size of the input:
Worst Case
If the array is sorted in strictly descending order, the algorithm compares each new element with all previous elements in the sorted subarray. The number of operations is quadratic: