High-performance sorting algorithms in C: Introsort (comparison sort) and Radix Sort (non-comparison sort), for integers, floating-point numbers and strings.
One DEFINE_INTROSORT(name, type, less) macro template instantiated for four element types:
introsort_i64(int64_t *a, size_t n)— general-purpose integer sort (negatives supported)introsort_dbl(double *a, size_t n)— floating-point sort (NaN is not supported)introsort_f32(float *a, size_t n)— single-precision floating-point sort (NaN is not supported)introsort_str(char **a, size_t n)— sorts an array of string pointers lexicographically (string data itself is not moved)
How it works:
- Quicksort core with median-of-three pivot selection (no degradation on sorted / nearly-sorted input)
- Switches to insertion sort for subarrays of ≤ 32 elements
- Falls back to heapsort once recursion depth exceeds 2·log₂n, guaranteeing O(n log n) worst case
radix_sort_u64(uint64_t *a, size_t n)— LSD, 8 bits per pass × 8 passesradix_sort_i64(int64_t *a, size_t n)— signed convenience wrapper (flips the sign bit and reuses the u64 core)radix_sort_dbl(double *a, size_t n)— maps IEEE-754 bit patterns to sortable integers (bitwise NOT for negatives, sets the sign bit for positives) and reuses the u64 core; NaN not supported eitherradix_sort_str(char **a, size_t n)— MSD, per-character counting sort (stable); shorter strings naturally come before longer ones sharing the same prefix; subarrays of ≤ 16 switch to insertion sort on suffixes; requires an auxiliary array of n × sizeof(char *)
make # or: gcc -O2 -Wall -Wextra -o fast_sort fast_sort.c
make test # correctness tests + benchmarks
make sanitize # ASan + UBSan build and runOn macOS, if the default SDK fails to link (e.g. tapi error: malformed file), pin an older SDK:
make SYSROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX15.4.sdkSingle-file library: drop fast_sort.c into your project and compile it with -DFAST_SORT_NO_MAIN to drop the built-in tests, benchmarks and main:
gcc -O2 -DFAST_SORT_NO_MAIN -c fast_sort.c#include <stddef.h>
double keys[] = {3.5, -1.0, 2.25};
introsort_dbl(keys, 3); /* => -1.0, 2.25, 3.5 */| Scenario | Recommended |
|---|---|
| General ints / floats / strings, unknown data distribution | Introsort (the safe default) |
| Huge volumes of plain integers (IDs, hashes, timestamps), enough RAM | Radix sort (fastest, O(n)) |
| Huge volumes of doubles | radix_sort_dbl (still O(n) after bit mapping) |
| Strings with many common prefixes | radix_sort_str (MSD skips shared prefixes naturally) |
| Small arrays (tens of thousands) | Either; introsort is simpler |
- Introsort: the all-rounder. Median-of-three keeps it from degrading on sorted input or on data with many duplicates; the heapsort fallback caps any input at O(n log n). New element types are one line away via the macro template (e.g.
float, struct pointers). - Radix sort: the specialist. The larger the dataset, the bigger its lead (4–6× faster than introsort at millions of elements); but it is slower on small arrays and needs extra memory. The double variant reuses the integer core via bit tricks; the string variant is an MSD sort that avoids repeatedly scanning shared prefixes.
int64 (5M random values):
| Method | Time |
|---|---|
| introsort | 0.228 s |
| stdlib qsort (reference) | 0.355 s |
| radix sort (uint64) | 0.060 s |
| radix sort (int64 wrapper) | 0.055 s |
double (5M random values):
| Method | Time |
|---|---|
| introsort | 0.307 s |
| stdlib qsort (reference) | 0.464 s |
| radix sort | 0.048 s |
float (5M random values):
| Method | Time |
|---|---|
| introsort | 0.307 s |
| stdlib qsort (reference) | 0.458 s |
string (1M random strings, length 1–12):
| Method | Time |
|---|---|
| introsort | 0.140 s |
| stdlib qsort (reference) | 0.154 s |
| radix sort (MSD) | 0.024 s |
v0.2 — float introsort, signed radix wrapper, library mode (FAST_SORT_NO_MAIN), CI, sanitizer target.
v0.1 — first release: introsort macro template (i64 / dbl / str), double & string radix sort, full test suite.