In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, radix sort is a non-
comparative
The degrees of comparison of adjectives and adverbs are the various forms taken by adjectives and adverbs when used to compare two entities (comparative degree), three or more entities (superlative degree), or when not comparing entities (positi ...
sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending ...
. It avoids comparison by creating and
distributing elements into buckets according to their
radix
In a positional numeral system, the radix (radices) or base is the number of unique digits, including the digit zero, used to represent numbers. For example, for the decimal system (the most common system in use today) the radix is ten, becaus ...
. For elements with more than one
significant digit
Significant figures, also referred to as significant digits, are specific Numerical digit, digits within a number that is written in positional notation that carry both reliability and necessity in conveying a particular quantity. When presen ...
, this bucketing process is repeated for each digit, while preserving the ordering of the prior step, until all digits have been considered. For this reason, radix sort has also been called
bucket sort and digital sort.
Radix sort can be applied to data that can be sorted
lexicographically, be they integers, words, punch cards, playing cards, or the
mail
The mail or post is a system for physically transporting postcards, letter (message), letters, and parcel (package), parcels. A postal service can be private or public, though many governments place restrictions on private systems. Since the mid ...
.
History
Radix sort dates back as far as 1887 to the work of
Herman Hollerith
Herman Hollerith (February 29, 1860 – November 17, 1929) was a German-American statistician, inventor, and businessman who developed an electromechanical tabulating machine for punched cards to assist in summarizing information and, later, in ...
on
tabulating machines. Radix sorting algorithms came into common use as a way to sort
punched card
A punched card (also punch card or punched-card) is a stiff paper-based medium used to store digital information via the presence or absence of holes in predefined positions. Developed over the 18th to 20th centuries, punched cards were widel ...
s as early as 1923.
[
]Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
. ''The Art of Computer Programming'', Volume 3: ''Sorting and Searching'', Third Edition. Addison-Wesley, 1997. . Section 5.2.5: Sorting by Distribution, pp. 168–179.
The first memory-efficient computer algorithm for this sorting method was developed in 1954 at
MIT
The Massachusetts Institute of Technology (MIT) is a private research university in Cambridge, Massachusetts, United States. Established in 1861, MIT has played a significant role in the development of many areas of modern technology and sc ...
by
Harold H. Seward
Harold H. Seward (July 24, 1930 – June 19, 2012) was a computer scientist, engineer, and inventor. Seward developed the radix sort and counting sort algorithms in 1954 at Massachusetts Institute of Technology, MIT. He also worked on the Whirlwi ...
. Computerized radix sorts had previously been dismissed as impractical because of the perceived need for variable allocation of buckets of unknown size. Seward's innovation was to use a linear scan to determine the required bucket sizes and offsets beforehand, allowing for a single static allocation of auxiliary memory. The linear scan is closely related to Seward's other algorithm —
counting sort.
In the modern era, radix sorts are most commonly applied to collections of binary
strings and
integers
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
. It has been shown in some benchmarks to be faster than other more general-purpose sorting algorithms, sometimes 50% to three times faster.
Digit order
Radix sorts can be implemented to start at either the
most significant digit (MSD) or
least significant digit (LSD). For example, with 1234, one could start with 1 (MSD) or 4 (LSD).
LSD radix sorts typically use the following sorting order: short keys come before longer keys, and then keys of the same length are sorted
lexicographically. This coincides with the normal order of integer representations, like the sequence
, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11''. LSD sorts are generally
stable sorts.
MSD radix sorts are most suitable for sorting strings or fixed-length integer representations. A sequence like
, c, e, d, f, g, ba'' would be sorted as
, ba, c, d, e, f, g''. If lexicographic ordering is used to sort variable-length integers in base 10, then numbers from 1 to 10 would be output as
, 10, 2, 3, 4, 5, 6, 7, 8, 9'', as if the shorter keys were left-justified and padded on the right with blank characters to make the shorter keys as long as the longest key. MSD sorts are not necessarily stable if the original ordering of duplicate keys must always be maintained.
Other than the traversal order, MSD and LSD sorts differ in their handling of variable length input.
LSD sorts can group by length, radix sort each group, then concatenate the groups in size order. MSD sorts must effectively 'extend' all shorter keys to the size of the largest key and sort them accordingly, which can be more complicated than the grouping required by LSD.
However, MSD sorts are more amenable to subdivision and recursion. Each bucket created by an MSD step can itself be radix sorted using the next most significant digit, without reference to any other buckets created in the previous step. Once the last digit is reached, concatenating the buckets is all that is required to complete the sort.
Examples
Least significant digit
Input list:
:
70, 45, 75, 90, 2, 802, 2, 66''
Starting from the rightmost (last) digit, sort the numbers based on that digit:
:
, , ''
Sorting by the next left digit:
:
, , , ''
:
Notice that an implicit digit ''0'' is prepended for the two 2s so that 802 maintains its position between them.
And finally by the leftmost digit:
:
, ''
:
Notice that a ''0'' is prepended to all of the 1- or 2-digit numbers.
Each step requires just a single pass over the data, since each item can be placed in its bucket without comparison with any other element.
Some radix sort implementations allocate space for buckets by first counting the number of keys that belong in each bucket before moving keys into those buckets. The number of times that each digit occurs is stored in an
array.
Although it is always possible to pre-determine the bucket boundaries using counts, some implementations opt to use dynamic memory allocation instead.
Most significant digit, forward recursive
Input list, fixed width numeric strings with leading zeros:
:
70, 045, 075, 025, 002, 024, 802, 066''
First digit, with brackets indicating buckets:
:
, ''
:
Notice that 170 and 802 are already complete because they are all that remain in their buckets, so no further recursion is needed
Next digit:
:
170, 802''
Final digit:
:
002, , 045, 066, 075 , 170, 802''
All that remains is concatenation:
:
02, 024, 025, 045, 066, 075, 170, 802''
Complexity and performance
Radix sort operates in
time, where
is the number of keys, and
is the key length. LSD variants can achieve a lower bound for
of 'average key length' when splitting variable length keys into groups as discussed above.
Optimized radix sorts can be very fast when working in a domain that suits them.
They are constrained to lexicographic data, but for many practical applications this is not a limitation. Large key sizes can hinder LSD implementations when the induced number of passes becomes the bottleneck.
Specialized variants
In-place MSD radix sort implementations
Binary MSD radix sort, also called binary quicksort, can be implemented in-place by splitting the input array into two bins - the 0s bin and the 1s bin. The 0s bin is grown from the beginning of the array, whereas the 1s bin is grown from the end of the array. The 0s bin boundary is placed before the first array element. The 1s bin boundary is placed after the last array element. The most significant bit of the first array element is examined. If this bit is a 1, then the first element is swapped with the element in front of the 1s bin boundary (the last element of the array), and the 1s bin is grown by one element by decrementing the 1s boundary array index. If this bit is a 0, then the first element remains at its current location, and the 0s bin is grown by one element. The next array element examined is the one in front of the 0s bin boundary (i.e. the first element that is not in the 0s bin or the 1s bin). This process continues until the 0s bin and the 1s bin reach each other. The 0s bin and the 1s bin are then sorted recursively based on the next bit of each array element. Recursive processing continues until the least significant bit has been used for sorting. Handling signed
two's complement
Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. Two's complement uses the binary digit with the ''greatest'' value as the ''s ...
integers requires treating the most significant bit with the opposite sense, followed by unsigned treatment of the rest of the bits.
In-place MSD binary-radix sort can be extended to larger radix and retain in-place capability.
Counting sort is used to determine the size of each bin and their starting index. Swapping is used to place the current element into its bin, followed by expanding the bin boundary. As the array elements are scanned the bins are skipped over and only elements between bins are processed, until the entire array has been processed and all elements end up in their respective bins. The number of bins is the same as the radix used - e.g. 16 bins for 16-radix. Each pass is based on a single digit (e.g. 4-bits per digit in the case of 16-radix), starting from the
most significant digit. Each bin is then processed recursively using the next digit, until all digits have been used for sorting.
Neither in-place binary-radix sort nor n-bit-radix sort, discussed in paragraphs above, are
stable algorithms.
Stable MSD radix sort implementations
MSD radix sort can be implemented as a stable algorithm, but requires the use of a memory buffer of the same size as the input array. This extra memory allows the input buffer to be scanned from the first array element to last, and move the array elements to the destination bins in the same order. Thus, equal elements will be placed in the memory buffer in the same order they were in the input array. The MSD-based algorithm uses the extra memory buffer as the output on the first level of recursion, but swaps the input and output on the next level of recursion, to avoid the overhead of copying the output result back to the input buffer. Each of the bins are recursively processed, as is done for the in-place MSD radix sort. After the sort by the last digit has been completed, the output buffer is checked to see if it is the original input array, and if it's not, then a single copy is performed. If the digit size is chosen such that the key size divided by the digit size is an even number, the copy at the end is avoided.
Hybrid approaches
Radix sort, such as the two-pass method where
counting sort is used during the first pass of each level of recursion, has a large constant overhead. Thus, when the bins get small, other sorting algorithms should be used, such as
insertion sort
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Howev ...
. A good implementation of insertion sort is fast for small arrays, stable, in-place, and can significantly speed up radix sort.
Application to parallel computing
This recursive sorting algorithm has particular application to
parallel computing
Parallel computing is a type of computing, computation in which many calculations or Process (computing), processes are carried out simultaneously. Large problems can often be divided into smaller ones, which can then be solved at the same time. ...
, as each of the bins can be sorted independently. In this case, each bin is passed to the next available processor. A single processor would be used at the start (the most significant digit). By the second or third digit, all available processors would likely be engaged. Ideally, as each subdivision is fully sorted, fewer and fewer processors would be utilized. In the worst case, all of the keys will be identical or nearly identical to each other, with the result that there will be little to no advantage to using parallel computing to sort the keys.
In the top level of recursion, opportunity for parallelism is in the
counting sort portion of the algorithm. Counting is highly parallel, amenable to the parallel_reduce pattern, and splits the work well across multiple cores until reaching memory bandwidth limit. This portion of the algorithm has data-independent parallelism. Processing each bin in subsequent recursion levels is data-dependent, however. For example, if all keys were of the same value, then there would be only a single bin with any elements in it, and no parallelism would be available. For random inputs all bins would be near equally populated and a large amount of parallelism opportunity would be available.
There are faster parallel sorting algorithms available, for example optimal complexity O(log(''n'')) are those of the Three Hungarians and Richard Cole and
Batcher's
bitonic merge sort has an algorithmic complexity of O(log
2(''n'')), all of which have a lower algorithmic time complexity to radix sort on a CREW-
PRAM. The fastest known PRAM sorts were described in 1991 by
David M W Powers with a parallelized quicksort that can operate in O(log(n)) time on a CRCW-PRAM with ''n'' processors by performing partitioning implicitly, as well as a radixsort that operates using the same trick in O(''k''), where ''k'' is the maximum keylength. However, neither the PRAM architecture or a single sequential processor can actually be built in a way that will scale without the number of constant
fan-out
In digital electronics, the fan-out is the number of gate inputs driven by the output of another single logic gate.
In most designs, logic gates are connected to form more complex circuits. While no logic gate input can be fed by more than one ...
gate delays per cycle increasing as O(log(''n'')), so that in effect a pipelined version of Batcher's bitonic mergesort and the O(log(''n'')) PRAM sorts are all O(log
2(''n'')) in terms of clock cycles, with Powers acknowledging that Batcher's would have lower constant in terms of gate delays than his Parallel
quicksort
Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961. It is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
and radix sort, or Cole's
merge sort
In computer science, merge sort (also commonly spelled as mergesort and as ) is an efficient, general-purpose, and comparison sort, comparison-based sorting algorithm. Most implementations of merge sort are Sorting algorithm#Stability, stable, wh ...
, for a keylength-independent
sorting network
In computer science, comparator networks are abstract devices built up of a fixed number of "wires", carrying values, and comparator modules that connect pairs of wires, swapping the values on the wires if they are not in a desired order. Such ne ...
of O(nlog
2(''n'')).
[David M. W. Powers]
Parallel Unification: Practical Complexity
Australasian Computer Architecture Workshop, Flinders University, January 1995
Tree-based radix sort
Radix sorting can also be accomplished by building a
tree
In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, e.g., including only woody plants with secondary growth, only ...
(or
radix tree
In computer science, a radix tree (also radix trie or compact prefix tree or compressed trie) is a data structure that represents a space-optimized trie (prefix tree) in which each node that is the only child is merged with its parent. The resu ...
) from the input set, and doing a
pre-order
A pre-order is an order placed for an item that has not yet been released. The idea for pre-orders came because people found it hard to get popular items in stores because of their popularity. Companies then had the idea to allow customers to r ...
traversal. This is similar to the relationship between
heapsort
In computer science, heapsort is an efficient, comparison-based sorting algorithm that reorganizes an input array into a heap (a data structure where each node is greater than its children) and then repeatedly removes the largest node from that ...
and the
heap data structure. This can be useful for certain data types, see
burstsort.
See also
*
IBM 80 series Card Sorters
* Other
distribution sorts
*
Kirkpatrick-Reisch sorting
*
Prefix sum
In computer science, the prefix sum, cumulative sum, inclusive scan, or simply scan of a sequence of numbers is a second sequence of numbers , the summation, sums of Prefix (computer science), prefixes (running totals) of the input sequence:
:
:
: ...
References
External links
Explanation, Pseudocode and implementationin C and Java
High Performance Implementationof LSD Radix sort in
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
High Performance Implementationof LSD & MSD Radix sort in
C# with source i
GitHubVideo tutorial of MSD Radix SortDemonstration and comparisonof Radix sort with
Bubble sort
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, Swap (computer science), swapping their values ...
,
Merge sort
In computer science, merge sort (also commonly spelled as mergesort and as ) is an efficient, general-purpose, and comparison sort, comparison-based sorting algorithm. Most implementations of merge sort are Sorting algorithm#Stability, stable, wh ...
and
Quicksort
Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961. It is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
implemented in
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
Articleabout Radix sorting
IEEE floating-point
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic originally established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many proble ...
numbers with implementation.
Faster Floating Point Sorting and Multiple Histogrammingwith implementation in C++
*Pointers t
radix sort visualizationsUSort library contains tuned implementations of radix sort for most numerical C types (C99)
*
Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
. ''The Art of Computer Programming'', Volume 3: ''Sorting and Searching'', Third Edition. Addison-Wesley, 1997. . Section 5.2.5: Sorting by Distribution, pp. 168–179.
*
Thomas H. Cormen,
Charles E. Leiserson,
Ronald L. Rivest, and
Clifford Stein
Clifford Seth Stein (born December 14, 1965), a computer scientist, is a professor of industrial engineering and operations research at Columbia University in New York, NY, where he also holds an appointment in the Department of Computer Scien ...
. ''
Introduction to Algorithms
''Introduction to Algorithms'' is a book on computer programming by Thomas H. Cormen, Charles E. Leiserson, Ron Rivest, Ronald L. Rivest, and Clifford Stein. The book is described by its publisher as "the leading algorithms text in universities w ...
'', Second Edition. MIT Press and McGraw-Hill, 2001. . Section 8.3: Radix sort, pp. 170–173.
BRADSORT v1.50 source code by Edward Lee. BRADSORT v1.50 is a radix sorting algorithm that combines a binary trie structure with a circular doubly linked list.
Efficient Trie-Based Sorting of Large Sets of Strings by Ranjan Sinha and Justin Zobel. This paper describes a method of creating tries of buckets which figuratively burst into sub-tries when the buckets hold more than a predetermined capacity of strings, hence the name, "Burstsort".
Pat Morin
*
ttp://opendatastructures.org/ods-cpp/11_2_Counting_Sort_Radix_So.html Open Data Structures - C++ Edition - Section 11.2 - Counting Sort and Radix Sort Pat Morin
{{DEFAULTSORT:Radix Sort
Articles with example C code
Sorting algorithms
Stable sorts
String sorting algorithms