Empirical Sorting Benchmark: Insertion Sort vs. Merge Sort Variants
C++17 benchmarking harness comparing standard and optimized implementations of Insertion Sort and Merge Sort across sorted, random, and reverse-sorted arrays up to n = 3,000.
Key Performance Metrics
Problem & Objective
Textbooks usually compare Insertion Sort and Merge Sort strictly by Big-O bounds (O(n^2) vs. O(n log n)), which hides both constant-factor behavior on smaller inputs and the impact of low-level implementation choices. This benchmark measures wall-clock execution time (minimum over 10 fresh-copy runs in microseconds), element comparisons, and swaps/merges across four C++17 (-O2) implementations: standard vs. sentinel-guarded Insertion Sort, and top-down recursive vs. bottom-up iterative Merge Sort over sorted, random, and reverse-sorted integer arrays (n = 500 to 3,000).
Technologies & Tools
Date
Feb 2026
Course
Design & Analysis of Algorithms
Testbed
C++17 (-O2), n = 500–3,000 (10-run min)
Findings
Key observations, empirical takeaways, and analytical conclusions.
- Eliminating the inner j >= 0 bounds check via a minimum-element sentinel cut Insertion Sort runtime by 21.4% on reverse-sorted data (2,837 → 2,230 µs) and 21.9% on random data (1,420 → 1,109 µs) at n = 3,000 with identical comparison counts.
- On already-sorted arrays, the sentinel upfront linear scan doubled comparisons (2,999 → 5,997) and slightly increased runtime (4 → 11 µs), though both Insertion Sort variants still outperformed Merge Sort by 45–123×.
- Bottom-up iterative Merge Sort outperformed top-down recursion by 13–15% across all three input distributions (579 → 490 µs reverse-sorted; 712 → 618 µs random) by avoiding recursive call overhead with virtually identical comparison counts.
- On random inputs, Insertion Sort remained faster than Merge Sort up to n ≈ 1,200–1,500 before quadratic scaling overtook its lower constant-factor overhead.
Results & Analysis
Empirical observations, comparative data, and experimental plots.
Tap to zoom
Minimum execution time over 10 runs across sorted, random, and reverse-sorted arrays, showing Insertion Sort adaptive O(n) behavior on sorted data, the n ≈ 1,200–1,500 crossover on random inputs, and quadratic divergence on reverse-sorted inputs.
Tap to zoom
Side-by-side runtime (µs) and log-scale comparison counts at n = 3,000, demonstrating that sentinel and bottom-up speedups come from loop/call overhead reduction rather than fewer element comparisons.