r/cpp • • Feb 19 '26

Experimental adaptive sort - matches std::sort on random input, 2-8x faster on structured data

34 Upvotes

Hi all,

I’ve been developing an adaptive sorting algorithm, tentatively called JesseSort, which aims to exploit partial order in input data while still being competitive with standard library sorts on random input. I’m looking for feedback on design and potential adoption strategies.

What it does

  • Detects natural runs in the input (ascending, descending, or random) with a tiny lookahead.
  • Maintains two sets of piles for ascending and descending runs, essentially a dual-patience sort.
  • Falls back to tiny 8-value bitonic sort networks on detected random regions.
  • When this random-input block is run too many times, it falls back to std::sort.
  • Currently merges adjacent runs in a naive/bottom-up way.

Current numbers

Median runtime ratios vs std::sort over 100 trials:

Input Type1k Values10k100k1M
Random0.9841.0321.0421.088
Sorted1.0220.6790.5831.448?
Reverse1.6361.0760.9002.101?
Sorted+Noise(5%)1.0481.0411.0791.201
Random+Repeats(50%)1.0371.0321.0311.089
Jitter1.0120.6740.5861.443?
Alternating0.8291.0110.9741.018
Sawtooth1.1210.9600.9781.072
BlockSorted1.0460.9500.9281.153
OrganPipe0.4460.2320.1380.268
Rotated0.5960.5220.3960.716
Signal1.4020.8280.6590.582

Notes:

  • Ratios are JesseSort / std::sort. Values <1 indicate JesseSort is faster. 0.5 means JesseSort takes half the time (2x faster). 2.0 means JesseSort takes twice as much time (2x slower).
  • Large input blow-ups (?) appear to be outliers on my machine, but would be curious to see if others see the same pattern.

Current issues / questions

  1. Handoff threshold: Detecting random input too early loses semi-structured gains; too late slows random input. How should this balance be tuned?
  2. Fallback vs. std::sort: Could JesseSort itself (dual patience games) serve as a better fallback than heap sort in standard introsort implementations?
  3. Merge optimizations: Current merge is bottom-up adjacent. I’ve prototyped a TimSort-style merge that merges smaller runs first. Minor speedups in most cases but I haven't tested it enough.
  4. Memory layout & cache: Some sensitivity to variable placement and data alignment is noticeable. Any advice for robust layout-sensitive optimizations?
  5. Real-world adoption: Even if slightly slower on purely random input (~5%), the structured input gains are often >50%. Would such an algorithm be worth promoting or considered niche? If the hit to random input is too significant, maybe this would find a better home as an alternative like std::structured_sort?

I’m looking for input on:

  • Algorithmic improvements, especially for the random vs structured handoff
  • Practical concerns for integration into standard libraries
  • Benchmark methodology for mixed input distributions
  • Real-world test datasets that might showcase advantages

Code and full details are available here: https://github.com/lewj85/jessesort

Thanks