Zhidaily AI · Intelligence · Daily
Zhidaily
Issue 11 · November 10, 2026 · Tue
Today's stories
12 stories · 6 companies
Home / November 10, 2026 / Author releases blqsort, a C++ quicksort faster than std::sort
← Back
Release News Score78

Author releases blqsort, a C++ quicksort faster than std::sort

In brief
The author wrote blqsort using branchless partitioning, sorting networks for small partitions and robust pivot selection, and reports it runs about twice as fast as std::sort. The claim is that general-purpose comparison sorting can still be faster on random data.
Lobsters tiki.li by chkas · RSS · tiki.li
Nov 9 · 20:55 Read the original ↗
01Summary

The standard library's std::sort is already fast, and rewriting a sort yourself usually gains only about ten percent; the author's plain recursive quicksort measured about 10% slower than std::sort. The author argues that modern CPU branch prediction and cache behaviour leave room: comparison outcomes during partitioning are hard to predict, and each misprediction flushes the pipeline, which is where general-purpose sorting loses time.

02Background

The standard library's std::sort is already fast, and rewriting a sort yourself usually gains only about ten percent; the author's plain recursive quicksort measured about 10% slower than std::sort. But the author argues that modern CPU branch prediction and cache behaviour leave room: comparison outcomes during partitioning are hard to predict, and each misprediction flushes the pipeline, which is exactly where general-purpose sorting loses time.

03Approach

The core idea is to replace "compare then jump" with "compare then compute". The author first illustrates this with an example that filters small numbers: the version with an if took 0.345 seconds on random data, while the branchless version took only 0.036 seconds, because the latter uses conditional increment instead of an unpredictable jump. Moving this trick into quicksort's partitioning, elements are written unconditionally first, and the comparison result then decides whether the write pointer advances, so partitioning no longer depends on branch prediction. Small partitions are no longer recursed into; instead they are dispatched by length to pre-written sorting networks: 2 to 16 elements each have a fixed compare-and-swap sequence, with no loop or branch overhead. Pivot selection is also more robust: small partitions take the median of 7 elements, while large partitions take several samples each from the first, middle and last positions and run multiple rounds of median filtering, with a random offset added so that a fixed pattern cannot be targeted by the input. The author also handles several degenerate cases: already-sorted and reverse-sorted inputs are handled efficiently; when there are many duplicate keys, a severely skewed partition is detected and elements equal to the pivot are first gathered together; two consecutive skewed partitions fall back to heapsort, ensuring the worst case does not run out of control. Measured on 50 million random doubles, blqsort took 0.67 seconds on an Apple M1, std::sort 1.33 seconds and ipnsort 0.99 seconds; on an Intel i5-9300H the times were 1.33, 4.97 and 2.31 seconds respectively. Switching to a struct containing an id and a value, blqsort still led, at 0.75 seconds on the M1 versus 3.45 seconds for std::sort. The author also acknowledges that for types such as strings, where copying is expensive and the type is not trivially copyable, the buffer-based branchless approach becomes less efficient; in that case a BlockQuicksort variant that applies branchless handling only to indices is used instead.

04Takeaways

The author's experience shows that the performance bottleneck of general-purpose comparison sorting is often not algorithmic complexity but branch prediction and cache behaviour; for scenarios dominated by random data with small elements, branchless implementations such as blqsort are worth evaluating as an alternative to std::sort. But the benchmarks cover only random data, and worst-case behaviour and memory overhead still need to be verified by users themselves.