Show HN: blqsort – A C++ Quicksort Faster Than std:sort tiki.li · 2 points · 0 c...

@show

Show HN: blqsort – A C++ Quicksort Faster Than std:sort tiki.li · 2 points · 0 comments · HN thread

TL;DR The author presents blqsort, a header-only C++ quicksort (MIT licensed, with a C version and multithreaded variants) that claims to beat std::sort and the ClickHouse ipnsort port on random data. The article walks through the techniques behind it: branchless partitioning, sorting networks for small partitions, median-based pivot selection and a heapsort fallback. Benchmarks on 50 million doubles show about 0.68s versus 1.33s for std::sort on an Apple M1.

  • Benchmark on 50M random doubles, Apple M1: blqsort 0.68s, ipnsort 1.00s, std::sort 1.33s; on an Intel i5-9300H: 1.32s, 2.11s, 4.02s.
  • For structs with an int key, blqsort takes 0.75s on the M1 versus 3.45s for std::sort, and the author says it is about three times faster again with the multithreaded variant.
  • Speedup comes from branchless partitioning with a buffer, sorting networks for partitions up to 16 elements, and a pivot chosen as a median of medians with some randomness.
  • Duplicate-heavy partitions are grouped, and heapsort is used as a fallback when partitioning stays uneven.
  • The article is a tutorial that starts from a naive recursive quicksort (4.61s) and builds up to the final header, which is used like std::sort.
  • The author notes that the approach is less efficient for non-trivially-copyable types such as strings, where a BlockQuicksort variant is used instead (C++17 required).

Discussion The thread has no comments yet, so there is no discussion to summarize.

#cpp #sorting #quicksort #performance #benchmark