Non-Comparison Sorts

Counting Sortnon comparisonhistogram, prefix sum, stable reverse placement. Radix Sortnon comparisondigit-by-digit passes, LSD versus MSD. Bucket Sortnon comparisondistribution into value ranges for floating-point data.

Sorts that beat O(n log n) by not comparing keys: counting, radix and bucket sort.

Bucket Sort

Bucket sort distributes elements into buckets by value, sorts each bucket, then concatenates. It runs in O(n+k) average time when elements are roughly…

Counting Sort

Counting sort is a non-comparison sort for integers in a small range [0, k). It tallies how often each value appears, computes a prefix sum to get final…

Radix Sort

Radix sort is a non-comparison sort that processes integer keys digit by digit, using a stable bucket sort at each digit. It runs in O(n·k) time where k is…

1. Overview

Any algorithm that sorts only by comparing pairs of keys needs Ω(n log n) comparisons in the worst case, because there are n! possible orderings and each comparison can at best halve the remaining possibilities. Non-comparison sorts sidestep that bound by inspecting the keys themselves: using a key as an array index, splitting it into digits, or mapping it to a value range. The price is an assumption about the keys, such as "small non-negative integers" or "uniformly distributed in [0, 1)". When the assumption holds, these sorts run in linear time; when it fails, they waste memory or degrade badly.

2. How the three relate

Non-comparison sorts build on one another Counting Sort index by key value Radix Sort counting sort per digit Bucket Sort distribute by value range Linear time when the key assumption holds

Counting sort is the primitive: it tallies occurrences of each integer key and computes final positions from a prefix sum. Radix sort applies a stable counting sort once per digit (least significant digit first), which keeps the counter array small even for 32- or 64-bit keys. Bucket sort takes a different route: it scatters values into buckets that cover equal slices of the key range, sorts each small bucket with an ordinary sort such as insertion sort, and concatenates. All three trade generality for speed.

3. Choosing between them

AlgorithmKeysTimeExtra spaceStableUse when
Counting SortIntegers in [min, max]O(n + k)O(n + k)YesRange k is about n or smaller
Radix SortFixed-width integers or stringsO(d · (n + b))O(n + b)Yes (LSD)Many keys with a wide range, few digits d in base b
Bucket SortReals, roughly uniformO(n) average, O(n²) worstO(n + buckets)If inner sort isData is evenly spread over a known range

In practice, general-purpose libraries use comparison sorts (Python's Timsort, introsort, pdqsort) because they make no assumptions. Non-comparison sorts appear inside specialised code: GPU sorting (radix sort is the standard GPU sort), database engines sorting fixed-width keys, histogram-based processing, and suffix-array construction. They are also a staple interview topic because they show why the n log n bound is a statement about a model of computation, not about sorting as such.

For the comparison-based sorts and an overall map of sorting algorithms, return to Algorithms.