Sorting
Insertion Sort
Build the sorted prefix one item at a time, shifting larger values right until the current key can be inserted.
Algorithm
The checked-in replay follows the same small input and final output across all 21 DSA books, so this C++ DSA implementation can be compared directly with the other languages.
sorted prefix
Positions before the scan index are already sorted.
shifting
Larger values move one slot right to make room for the key.
Basic Implementation
basic.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <vector>
int main() {
std::vector<int> arr{5, 1, 4, 2, 8};
for (size_t i = 1; i < arr.size(); ++i) {
int key = arr[i];
int j = static_cast<int>(i) - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
std::cout << "[";
for (size_t i = 0; i < arr.size(); ++i) {
if (i > 0) std::cout << ", ";
std::cout << arr[i];
}
std::cout << "]" << std::endl;
return 0;
}
arr ← [5, 1, 4, 2, 8]
1#include <iostream>2#include <vector>values this step[5, 1, 4, 2, 8]arrarr ← [1, 5, 4, 2, 8]
6for (size_t i = 1; i < arr.size(); ++i) {7 int key = arr[i];8 int j = static_cast<int>(i) - 1;values this step[5, 1, 4, 2, 8] → [1, 5, 4, 2, 8]arr1keyarr ← [1, 4, 5, 2, 8]
8int j = static_cast<int>(i) - 1;9while (j >= 0 && arr[j] > key) {10 arr[j + 1] = arr[j];values this step[1, 5, 4, 2, 8] → [1, 4, 5, 2, 8]arr4keyarr ← [1, 2, 4, 5, 8]
8int j = static_cast<int>(i) - 1;9while (j >= 0 && arr[j] > key) {10 arr[j + 1] = arr[j];values this step[1, 4, 5, 2, 8] → [1, 2, 4, 5, 8]arr2keystdout ← [1, 2, 4, 5, 8]
1#include <iostream>2#include <vector>values this step[1, 2, 4, 5, 8]stdout[1, 2, 4, 5, 8]arr
Complexity
- Time: O(n^2) worst and average, O(n) best
- Space: O(1)
- Stable: yes
Implementation notes
- In C++, the input is a
std::vector<int>initialized as{5, 1, 4, 2, 8}and sorted in place; no second vector is allocated. - The outer loop uses
size_t ifrom1toarr.size() - 1, while the backward scan usesint j = static_cast<int>(i) - 1so it can testj >= 0before readingarr[j]. int key = arr[i]copies the current vector element into a scalar temporary before shifts overwrite later slots.- The while loop shifts with
arr[j + 1] = arr[j]whilearr[j] > key, then writes the saved key back witharr[j + 1] = key. - The trace records
[5, 1, 4, 2, 8], then insertions to[1, 5, 4, 2, 8],[1, 4, 5, 2, 8], and[1, 2, 4, 5, 8]; the final8needs no visible shift. - Output is streamed with
std::cout, asize_tprint loop, comma separators, andstd::endl, producing[1, 2, 4, 5, 8]. Visible allocation is the vector storage from the initializer list; mutation is limited to vector element assignments and the scalarkey/jlocals.