Radix sort is an integer sorting algorithm that sorts data with integer keys by grouping the keys by individual digits that share the same significant position and value (place value). Radix sort uses counting sort as a subroutine to sort an array of numbers. Because integers can be used to represent strings (by hashing the strings to integers), radix sort works on data types other than just integers. Because radix sort is not comparison based, it is not bounded by for running time — in fact, radix sort can perform in linear time.
Radix sort incorporates the counting sort algorithm so that it can sort larger, multi-digit numbers without having to potentially decrease the efficiency by increasing the range of keys the algorithm must sort over (since this might cause a lot of wasted time).
Radix sort takes in a list of integers which are in base (the radix) and so each number has at most digits where and is the largest number in the list. For example, three digits are needed to represent decimal (in base 10). It is important that radix sort can work with any base since the running time of the algorithm, , depends on the base it uses. The algorithm runs in linear time when and are of the same size magnitude, so knowing , can be manipulated to optimize the running time of the algorithm.
What is decimal in binary (base 2).
Here is a table describing the digits of the decimal base. In the th place, there are 1's, in the place there are 10's, and so on.
We are pretty used to the decimal system, so it is easy to see that we need a in the 10's place and a in the 1's place to make . Let's make a similar table for binary.
To make decimal in binary, we can add which corresponds to a in the 1's, 2's, and 4's places, and a in all the other places. In binary, decimal can be represented as .
Radix sort works by sorting each digit from least significant digit to most significant digit. So in base 10 (the decimal system), radix sort would sort by the digits in the 1's place, then the 10’s place, and so on. To do this, radix sort uses counting sort as a subroutine to sort the digits in each place value. This means that for a three-digit number in base 10, counting sort will be called to sort the 1's place, then it will be called to sort the 10's place, and finally, it will be called to sort the 100's place, resulting in a completely sorted list. Here is a quick refresher on the counting sort algorithm.
Counting Sort Subroutine
Counting sort uses three lists: the input list, , the output list, , and a list that serves as temporary memory, . Note that and have slots (a slot for each element), while contains slots (a slot for each key value).
Counting sort starts by going through , and for each element , it goes to the index of that has the same value as (so it goes to ) and increments the value of by one. This means that if has seven ’s in its list, after counting sort has gone through all elements of , the value at will be . Similarly, if has two ’s, after counting sort has gone through all of the elements of , (using 0 indexing) will be equal to .
In this step, keeps track of how many elements in there are that have the same value of a particular index in . In other words, the indices of correspond to the values of elements in , and the values in correspond to the total number of times that a value in appears in .
Radix sort is a stable sort, which means it preserves the relative order of elements that have the same key value. This is very important.
For example, since the list of numbers will be sorted as when the 1’s place is sorted (since ) and on the second pass, when the 10’s place is being sorted, the sort will see that three of the four values are . To preserve the sorting that the algorithm determined while sorting the 1’s place, it is important to maintain relative order (namely ) between the numbers with the same value in the 10’s place (or whatever place value is currently being sorted). The second pass of the radix sort will produce .
Counting sort can only sort one place value of a given base. For example, a counting sort for base-10 numbers can only sort digits zero through nine. To sort two-digit numbers, counting sort would need to operate in base-100. Radix sort is more powerful because it can sort multi-digit numbers without having to search over a wider range of keys (which would happen if the base was larger).
In the image showing radix sort below, notice that each column of numbers (each place value) is sorted by the digit in question before the algorithm moves on to the next place value. This shows how radix sort preserves the relative order of digits with the same value at a given place value — remember, and will both appear as 's in the 10's column, but , so the order determined in the 1's column, that must be preserved for the sort to work properly and produce the correct answer.
Here is a Python implementation of radix sort that explicitly uses counting sort as a subroutine (some implementations combine the functions).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
Radix sort will operate on -digit numbers where each digit can be one of at most different values (since is the base being used). For example, in base 10, a digit can be .
Radix sort uses counting sort on each digit. Each pass over -digit numbers will take time, and there are passes total. Therefore, the total running time of radix sort is . When is a constant and isn't much larger than (in other words, ), then radix sort takes linear time.
- Demaine, E., & Devadas, S. Lecture 7: Counting Sort, Radix Sort, Lower Bounds for Sorting. Retrieved May 22, 2016, from http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec07_orig.pdf