Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
B

blitsort

> 编程语言
Open source

Blitsort is an in-place stable adaptive rotate mergesort / quicksort.

713 stars0 likes0 views
WebsiteGitHub

About

Blitsort is an in-place stable adaptive rotate mergesort / quicksort.

Origin

Blitsort is an in-place rotate quick/merge sort based on the stable out-of-place merge sort quadsort, stable out-of-place quicksort fluxsort, and unstable in-place crumsort. This page primarily focuses on the in-place stable aspects of blitsort, the quadsort and fluxsort pages go into greater detail about mergesort and stable quicksort respectively.

Visualization

In the visualization below the upper half shows the swap memory (32 elements) and the bottom half shows the main memory (512 elements). Colors are used to differentiate between various operations. Partitioning is in light and dark blue, rotations are in yellow and violet. The blitsort visualization only shows the sorting of random data, the sorting of ordered data is identical to the quadsort visualization on the quadsort github page.

Analyzer

Blitsort uses the same analyzer as fluxsort. The distribution is scanned to determine how much of it is already sorted. Distributions that are fully in order or in reverse-order are handled immediately. Segments that are mostly sorted are sorted with rotate quadsort, otherwise they're sorted with rotate fluxsort.

Rotate merge sort

A rotate merge sort uses rotations to partition two sorted arrays until they're small enough to be merged using auxiliary memory. Blitsort does so by taking the center element of the first array, using a binary search to find all elements smaller than the center element in the second array, and performing an array rotation. It does so recursively until a partition becomes small enough to be merged.

Rotate stable quicksort

A stable rotate quicksort partitions the array in segments using auxiliary memory, the left and right partition of each segment is then moved to the proper location recursively using rotations. On random data it's faster than a rotate mergesort since no binary search is required and because the branchless optimizations are slightly more efficient.

Monobound binary search

Blitsort uses a monobound binary search, which is up to two times faster than the binary search in general use.

Trinity rotation

Blitsort uses a trinity rotation, a new and significantly faster array rotation algorithm.

Memory

By default blitsort uses 512 elements worth of stack memory.

The minimum memory requirement for blitsort is 32 elements of stack memory, it can be configured to use sqrt(n) memory.

Blitsort partitions recursively, requiring an additional log(n) memory. It's possible to make this O(1) through the implementation of a stack, which makes the rotate quick/merge sort algorithm in-place from a theoretical perspective.

There is currently no clear consensus on what constitutes as an in-place sort, it boils down to what someone considers a small enough memory footprint to be considered negligable. This typically ranges from the size of a cache line to the size of the L1 cache.

Performance

Blitsort has exceptional performance due to the use of trinity / bridge rotations. It is likely the fastest in-place stable sort written so far and is about 15-50% faster than octosort, which is a block merge sort.

Blitsort's performance is similar to that of fluxsort as long as it has sqrt(n) auxiliary memory. Performance on larger arrays degrades steadily but will still beat most traditional sorts at 10 million elements.

To take full advantage of branchless operations the cmp macro needs to be uncommented in bench.c, which will double the performance on primitive types. The blitsort_prim function can be used to access primitive comparisons directly for 32 and 64 bit integers.

Data Types

Blitsort supports long doubles and 8, 16, 32, and 64 bit data types. By using 32 or 64 bit pointers it's possible to sort any other data type, like strings. Custom data sizes can be added in blitsort.h. For any practical use where stability matters you'll likely be using pointers however.

Interface

The interface is the same one as qsort, which is described in man qsort.

Big O

…

Benchmark: blitsort vs std::stable_sort vs gfx::timsort

The following benchmark was on WSL 2 gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) using the wolfsort benchmark. The source code was compiled using g++ -O3 -w -fpermissive bench.c. Stablesort is g++'s std:stable_sort function.

The graph shows the relative performance on 100,000 32 bit integers. A table with the best and average time in seconds can be uncollapsed below the bar graph.

data table

Name Items Type Best Average Compares Samples Distribution
stablesort 100000 128 0.010913 0.011047 0 100 random order
blitsort 100000 128 0.010880 0.011423 0 100 random order
timsort 100000 128 0.012791 0.012901 0 100 random order
Name Items Type Best Average Compares Samples Distribution
stablesort 100000 64 0.006125 0.006207 0 100 random order
blitsort 100000 64 0.002397 0.002518 0 100 random order
timsort 100000 64 0.007787 0.007855 0 100 random order
Name Items Type Best Average Loops Samples Distribution
stablesort 100000 32 0.005991 0.006025 0 100 random order
blitsort 100000 32 0.002245 0.002341 0 100 random order
timsort 100000 32 0.007620 0.007656 0 100 random order
stablesort 100000 32 0.003815 0.003838 0 100 random % 100
blitsort 100000 32 0.001017 0.001085 0 100 random % 100
timsort 100000 32 0.005641 0.005674 0 100 random % 100
stablesort 100000 32 0.000673 0.000703 0 100 ascending order
blitsort 100000 32 0.000044 0.000044 0 100 ascending order
timsort 100000 32 0.000045 0.000045 0 100 ascending order
stablesort 100000 32 0.001363 0.001399 0 100 ascending saw
blitsort 100000 32 0.000628 0.000636 0 100 ascending saw
timsort 100000 32 0.000844 0.000849 0 100 ascending saw
stablesort 100000 32 0.000810 0.000840 0 100 pipe organ
blitsort 100000 32 0.000358 0.000363 0 100 pipe organ
timsort 100000 32 0.000169 0.000172 0 100 pipe organ
stablesort 100000 32 0.000905 0.000921 0 100 descending order
blitsort 100000 32 0.000055 0.000055 0 100 descending order
timsort 100000 32 0.000088 0.000092 0 100 descending order
stablesort 100000 32 0.001361 0.001402 0 100 descending saw
blitsort 100000 32 0.000637 0.000641 0 100 descending saw
timsort 100000 32 0.000842 0.000850 0 100 descending saw
stablesort 100000 32 0.002033 0.002090 0 100 random tail
blitsort 100000 32 0.000956 0.000961 0 100 random tail
timsort 100000 32 0.001999 0.002019 0 100 random tail
stablesort 100000 32 0.003493 0.003541 0 100 random half
blitsort 100000 32 0.001396 0.001404 0 100 random half
timsort 100000 32 0.004033 0.004059 0 100 random half
stablesort 100000 32 0.000921 0.000948 0 100 ascending tiles
blitsort 100000 32 0.001383 0.001397 0 100 ascending tiles
timsort 100000 32 0.000836 0.000880 0 100 ascending tiles
stablesort 100000 32 0.001171 0.001355 0 100 bit reversal
blitsort 100000 32 0.002052 0.002160 0 100 bit reversal
timsort 100000 32 0.002250 0.002832 0 100 bit reversal

The following benchmark was on WSL 2 gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) using the wolfsort benchmark. The source code was compiled using g++ -O3 -w -fpermissive bench.c. It measures the performance on random data with array sizes ranging from 10 to 10,000,000. It's generated by running the benchmark using 1024 0 0 as the argument. The benchmark is weighted, meaning the number of repetitions halves each time the number of items doubles. A table with the best and average time in seconds can be uncollapsed below the bar graph.

data table

Name Items Type Best Average Compares Samples Distribution
stablesort 10 32 0.126263 0.126498 0.0 10 random 10
blitsort 10 32 0.050169 0.050964 0.0 10 random 10
timsort 10 32 0.140403 0.142217 0.0 10 random 10
stablesort 100 32 0.245318 0.245592 0.0 10 random 100
blitsort 100 32 0.113215 0.113717 0.0 10 random 100
timsort 100 32 0.344290 0.344927 0.0 10 random 100
stablesort 1000 32 0.362981 0.363514 0.0 10 random 1000
blitsort 1000 32 0.141162 0.141663

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Calgorithmbranchlesscinplace

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言