csort

A fast, branchless, sorting algorithm

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version 1.0.0
License Unlicense
CI Status Failing
Downloads 0
Last Indexed 2026-07-21 05:27

Tags

Authors

  • WyattBlue

Installation

nimble install csort
choosenim install csort
git clone https://github.com/WyattBlue/csort

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
csort - - - - - - -

Dependencies

Package Version Optional
nim >= 2.2.4 No

Source

Repository https://github.com/WyattBlue/csort
Homepage https://github.com/WyattBlue/csort
Registry Source nimble_official

README

csort

csort is as constant-time sorting network. By using SIMD instructions, it achieves 2-5x faster times than Nim's std/sort for reasonable sized arrays.

By being in constant-time, no matter what the data is, it makes it immune to timing side-channels. This matters if you need to sort sensitive data in cryptographic contexts.

Usage

nimble install csort
import csort 

var a = @[3, 2, 5, 1, 4]
a.sort()
echo a

> @[1, 2, 3, 4, 5]

Performance

The following benchmarks compare csort with Nim's standard library sort.

AArch64 MacOS Benchmark:

n int32 speedup int64 speedup
10,000 5.4× 3.4×
100,000 4.7× 2.8×
1,000,000 4.0× 1.7×

For sequences bigger than a million, the network scaling O(n log^2 n) catches up.

Prior Art

Based on Ken Batcher's 1968 paper and Daniel J. Bernstein's work.