r/cpp • u/booker388 • Feb 19 '26
Experimental adaptive sort - matches std::sort on random input, 2-8x faster on structured data
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 Type | 1k Values | 10k | 100k | 1M |
|---|---|---|---|---|
| Random | 0.984 | 1.032 | 1.042 | 1.088 |
| Sorted | 1.022 | 0.679 | 0.583 | 1.448? |
| Reverse | 1.636 | 1.076 | 0.900 | 2.101? |
| Sorted+Noise(5%) | 1.048 | 1.041 | 1.079 | 1.201 |
| Random+Repeats(50%) | 1.037 | 1.032 | 1.031 | 1.089 |
| Jitter | 1.012 | 0.674 | 0.586 | 1.443? |
| Alternating | 0.829 | 1.011 | 0.974 | 1.018 |
| Sawtooth | 1.121 | 0.960 | 0.978 | 1.072 |
| BlockSorted | 1.046 | 0.950 | 0.928 | 1.153 |
| OrganPipe | 0.446 | 0.232 | 0.138 | 0.268 |
| Rotated | 0.596 | 0.522 | 0.396 | 0.716 |
| Signal | 1.402 | 0.828 | 0.659 | 0.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
- Handoff threshold: Detecting random input too early loses semi-structured gains; too late slows random input. How should this balance be tuned?
- Fallback vs. std::sort: Could JesseSort itself (dual patience games) serve as a better fallback than heap sort in standard introsort implementations?
- 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.
- Memory layout & cache: Some sensitivity to variable placement and data alignment is noticeable. Any advice for robust layout-sensitive optimizations?
- 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
35
Upvotes
•
u/STL MSVC STL Dev Feb 19 '26
We're drowning in AI-generated half-baked project posts (doubly off-topic), so I will approve your post as a special exception since it appears to be fully human-written and developing a new algorithm. We usually want project posts to go into show&tell, and code review questions to go to r/cpp_questions, hence the exception.
20
u/tialaramex Feb 19 '26
As somebody else pointed out you need to specify which exact implementation of
std::sortyou're comparing against. Named compiler, stdlib and platform, probably also target hardware. None of the popular STLs is close to state-of-the-art performance but they vary considerably and that sets expectations.You should also mention if your code has safety problems, several STLs are either actively working to remove such problems or have a moratorium on adding more, if you can't imagine what a safety problem for a C++ sort would look like there's a talk: https://www.youtube.com/watch?v=rZ7QQWKP8Rk
The cases where JesseSort was markedly worse than your
std::sortare a problem and need explaining before anybody would want this. I'd say there's a good chance it's simply a bug in your software.11
u/mapronV Feb 19 '26 edited Feb 19 '26
Can you please:
(I hope it will not end as previous post which author deleted after my benchmark request).
p.s. Would nice to see CMakeLists.txt added to your repo, I don't have Cython installed on my pc, so for project I have 0 interest, I want bother installing extra tools. However with cmake I could run cmake/build/run myself. It's not a blame, just a wish as CMake is the most popular build system for C++ projects.
8
u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 19 '26
Which std::sort are you using? In my understanding, libc++ already does several of these tricks.
3
u/Morwenn Feb 19 '26
I did a partial comparison of the algorithm to existing ones a while ago after an implementation request by the author: https://github.com/Morwenn/cpp-sort/issues/224
Still have to implement it, but I find the comparisons relevant, and feedback from the author would still be welcome just to make sure that I'm not entirely mistaken.
2
u/[deleted] Feb 19 '26
What's the worst-case sort time? I.e. when the lookahead/prediction is wrong. In some cases you need guarantees on upper limit for the time it takes.
2
u/Fabulous-Meaning-966 Feb 20 '26
Maybe check out some of this guy's projects: https://github.com/scandum/
2
u/_Noreturn Feb 19 '26
Jesse time to sort.
is the readme ai?
1
u/thisismyfavoritename Feb 19 '26
probably just need to test with google benchmark