Skip to main content
All work
Case Study // Benchmarks

Algorithmic Optimization

Eight sorts and five searches in C++, from textbook versions to versions tuned by measurement, with a benchmark that times them on the same inputs.

Role
Sole author
When
2024 – 2026
Stack
C++20, CMake, raylib, JavaScript
Status
Version 2, October 2026

What it is

Complexity analysis predicts how many comparisons an algorithm makes. It says much less about how long the algorithm takes on a real processor. This project measures that difference, one algorithm and one optimization at a time.

It started in 2024 as my SOP, a Danish upper-secondary study project in mathematics and programming. That first version measured the wrong things: its quicksort created a new random number generator on every partition, and its binary-search benchmark included printing to the console in its timing. Version 2, published on 2 October 2026, rebuilds the project around the original question without those mistakes.

My part

I wrote the library, the benchmark, the tests, the written analysis, a browser visualizer and a raylib results viewer: 112 commits between September 2024 and October 2026. The adversarial quicksort input implements M. D. McIlroy's “A Killer Adversary for Quicksort” (1999), the introsort follows D. R. Musser's design (1997), and the benchmark's trace writer grew out of The Cherno's instrumentation profiler.

How it measures

  • The textbook and tuned versions run in the same benchmark, on the same inputs.
  • Random inputs come from SplitMix64, so every platform sorts identical arrays.
  • Preparing the inputs and counting comparisons both happen outside the timed part.
  • A wrong answer stops the run: the last timed repetition of each sort has to match std::sort.
  • CI builds and tests it with GCC and Clang on Linux, Apple Clang on macOS and MSVC on Windows.

What the numbers show

Each row compares a faster version with what it replaces, on one input.

ComparisonInputBeforeAfter
Tuned merge sort vs textbook: one buffer instead of two allocations per merge, insertion sort up to 32 elements1M random107 ns/element53 ns/element
The same pair on sorted input, where the tuned version skips merging runs that are already in order1M sorted54 ns/element0.9 ns/element
Tuned quicksort vs textbook: median-of-three pivot, Hoare partition, insertion sort for small ranges32K sorted4,398 ns/element2.4 ns/element
Introsort vs the tuned quicksort, on input built to defeat quicksort32K adversarial1,647 ns/element45 ns/element
Radix sort vs std::sort1M random47 ns/element3.4 ns/element
Branchless binary search vs std::lower_bound1M keys117 ns/lookup49 ns/lookup
Eytzinger layout vs std::lower_bound on a sorted array64M keys397 ns/lookup56 ns/lookup
One run on an AMD Ryzen 7 9800X3D under Windows, GCC 15.2 at -O3, 32-bit integer keys, 2 October 2026. Medians, from the repository's README and committed results. Your numbers will differ.

The comparison counts and the running times disagree. The textbook merge sort made 19,645,004 comparisons on a million random integers, within 1% of the minimum for any comparison sort. It was still the slowest comparison sort measured, because it allocates two new vectors for every merge.

Searching is about memory more than comparisons. The binary searches in the table make about the same number of probes, and their times climb as the array outgrows each level of the processor's cache.

Decisions

Keep the textbook version

Each tuned version is timed against its textbook form or the standard library in the same run, not against a remembered number.

Trade-offThe textbook versions are slow on purpose. The quadratic cases stop at 32K elements, and the benchmark needs a 64 MiB stack on Windows and macOS because the textbook quicksort recurses once per element on sorted input.

Generate inputs with SplitMix64

The C++ standard leaves the exact output of its random distributions to each library, so the same seed could give different arrays on different platforms. SplitMix64 is fully specified.

Stop on a wrong answer

A row is only written after its output passes a check against the standard library.

Trade-offOnly the final timed repetition of each sort is checked, and searches are checked by a sum rather than one result at a time.

Limits

  • One machine and one run. The benchmark in CI is a quick check and doesn't reproduce these numbers.
  • Each value is the median of at least five repetitions, with no error bars, so small differences are noise.
  • Search times are throughput over 65,536 lookups, not the latency of a single lookup.

Outcome

Comparison counts follow the textbook formulas, exactly in the worst cases and closely on random input. Running times don't, and the analysis puts the difference down to memory allocation, branch mispredictions and memory access.