The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is Ω(nLogn), i.e., they cannot do better than nLogn.

Counting sort is a linear time sorting algorithm that sort in O(n+k) time when elements are in range from 1 to k.

What if the elements are in range from 1 to n2?
We can’t use counting sort because counting sort will take O(n2) which is worse than comparison based sorting algorithms. Can we sort such an array in linear time?
Radix Sort is the answer. The idea of Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix sort uses counting sort as a subroutine to sort.

The Radix Sort Algorithm
1) Do following for each digit i where i varies from least significant digit to the most significant

2) Sort input array using counting sort (or any stable sort) according to the i’th digit.

Example:
Original, unsorted list:

170, 45, 75, 90, 802, 24, 2, 66

Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]

170, 90, 802, 2, 24, 45, 75, 66

Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]

802, 2, 24, 45, 66, 170, 75, 90

Sorting by most significant digit (100s place) gives:

2, 24, 45, 66, 75, 90, 170, 802
[ad type=”banner”]

What is the running time of Radix Sort?
Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for decimal system, b is 10. What is the value of d? If k is the maximum possible value, then d would be O(logb(k)). So overall time complexity is O((n+b) * logb(k)). Which looks more than the time complexity of comparison based sorting algorithms for a large k. Let us first limit k. Let k <= nc where c is a constant. In that case, the complexity becomes O(nLogb(n)). But it still doesn’t beat comparison based sorting algorithms.
What if we make value of b larger?. What should be the value of b to make the time complexity linear? If we set b as n, we get the time complexity as O(n). In other words, we can sort an array of integers with range from 1 to nc if the numbers are represented in base n (or every digit takes log2(n) bits).

Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?
If we have log2n bits for every digit, the running time of Radix appears to be better than Quick Sort for a wide range of input numbers. The constant factors hidden in asymptotic notation are higher for Radix Sort and Quick-Sort uses hardware caches more effectively. Also, Radix sort uses counting sort as a subroutine and counting sort takes extra space to sort numbers.

Implementation of Radix Sort
Following is a simple C++ implementation of Radix Sort. For simplicity, the value of d is assumed to be 10. We recommend you to see Counting Sort for details of countSort() function in below code.

C

[pastacode lang=”c” manual=”%2F%2F%20C%2B%2B%20implementation%20of%20Radix%20Sort%0A%23include%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20get%20maximum%20value%20in%20arr%5B%5D%0Aint%20getMax(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20mx%20%3D%20arr%5B0%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3E%20mx)%0A%20%20%20%20%20%20%20%20%20%20%20%20mx%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20return%20mx%3B%0A%7D%0A%20%0A%2F%2F%20A%20function%20to%20do%20counting%20sort%20of%20arr%5B%5D%20according%20to%0A%2F%2F%20the%20digit%20represented%20by%20exp.%0Avoid%20countSort(int%20arr%5B%5D%2C%20int%20n%2C%20int%20exp)%0A%7B%0A%20%20%20%20int%20output%5Bn%5D%3B%20%2F%2F%20output%20array%0A%20%20%20%20int%20i%2C%20count%5B10%5D%20%3D%20%7B0%7D%3B%0A%20%0A%20%20%20%20%2F%2F%20Store%20count%20of%20occurrences%20in%20count%5B%5D%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20count%5B%20(arr%5Bi%5D%2Fexp)%2510%20%5D%2B%2B%3B%0A%20%0A%20%20%20%20%2F%2F%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%2F%2F%20%20position%20of%20this%20digit%20in%20output%5B%5D%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%2010%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi%20-%201%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Build%20the%20output%20array%0A%20%20%20%20for%20(i%20%3D%20n%20-%201%3B%20i%20%3E%3D%200%3B%20i–)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20output%5Bcount%5B%20(arr%5Bi%5D%2Fexp)%2510%20%5D%20-%201%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20count%5B%20(arr%5Bi%5D%2Fexp)%2510%20%5D–%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Copy%20the%20output%20array%20to%20arr%5B%5D%2C%20so%20that%20arr%5B%5D%20now%0A%20%20%20%20%2F%2F%20contains%20sorted%20numbers%20according%20to%20current%20digit%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20output%5Bi%5D%3B%0A%7D%0A%20%0A%2F%2F%20The%20main%20function%20to%20that%20sorts%20arr%5B%5D%20of%20size%20n%20using%20%0A%2F%2F%20Radix%20Sort%0Avoid%20radixsort(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Find%20the%20maximum%20number%20to%20know%20number%20of%20digits%0A%20%20%20%20int%20m%20%3D%20getMax(arr%2C%20n)%3B%0A%20%0A%20%20%20%20%2F%2F%20Do%20counting%20sort%20for%20every%20digit.%20Note%20that%20instead%0A%20%20%20%20%2F%2F%20of%20passing%20digit%20number%2C%20exp%20is%20passed.%20exp%20is%2010%5Ei%0A%20%20%20%20%2F%2F%20where%20i%20is%20current%20digit%20number%0A%20%20%20%20for%20(int%20exp%20%3D%201%3B%20m%2Fexp%20%3E%200%3B%20exp%20*%3D%2010)%0A%20%20%20%20%20%20%20%20countSort(arr%2C%20n%2C%20exp)%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20an%20array%0Avoid%20print(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B170%2C%2045%2C%2075%2C%2090%2C%20802%2C%2024%2C%202%2C%2066%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20radixsort(arr%2C%20n)%3B%0A%20%20%20%20print(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/] [ad type=”banner”]

JAVA

[pastacode lang=”java” manual=”%2F%2F%20Radix%20sort%20Java%20implementation%0Aimport%20java.io.*%3B%0Aimport%20java.util.*%3B%0A%20%0Aclass%20Radix%20%7B%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20function%20to%20get%20maximum%20value%20in%20arr%5B%5D%0A%20%20%20%20static%20int%20getMax(int%20arr%5B%5D%2C%20int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20mx%20%3D%20arr%5B0%5D%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3E%20mx)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mx%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20return%20mx%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20function%20to%20do%20counting%20sort%20of%20arr%5B%5D%20according%20to%0A%20%20%20%20%2F%2F%20the%20digit%20represented%20by%20exp.%0A%20%20%20%20static%20void%20countSort(int%20arr%5B%5D%2C%20int%20n%2C%20int%20exp)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20output%5B%5D%20%3D%20new%20int%5Bn%5D%3B%20%2F%2F%20output%20array%0A%20%20%20%20%20%20%20%20int%20i%3B%0A%20%20%20%20%20%20%20%20int%20count%5B%5D%20%3D%20new%20int%5B10%5D%3B%0A%20%20%20%20%20%20%20%20Arrays.fill(count%2C0)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Store%20count%20of%20occurrences%20in%20count%5B%5D%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20count%5B%20(arr%5Bi%5D%2Fexp)%2510%20%5D%2B%2B%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%0A%20%20%20%20%20%20%20%20%2F%2F%20actual%20position%20of%20this%20digit%20in%20output%5B%5D%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%2010%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi%20-%201%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Build%20the%20output%20array%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%20n%20-%201%3B%20i%20%3E%3D%200%3B%20i–)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20output%5Bcount%5B%20(arr%5Bi%5D%2Fexp)%2510%20%5D%20-%201%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20count%5B%20(arr%5Bi%5D%2Fexp)%2510%20%5D–%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Copy%20the%20output%20array%20to%20arr%5B%5D%2C%20so%20that%20arr%5B%5D%20now%0A%20%20%20%20%20%20%20%20%2F%2F%20contains%20sorted%20numbers%20according%20to%20curent%20digit%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20output%5Bi%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20The%20main%20function%20to%20that%20sorts%20arr%5B%5D%20of%20size%20n%20using%0A%20%20%20%20%2F%2F%20Radix%20Sort%0A%20%20%20%20static%20void%20radixsort(int%20arr%5B%5D%2C%20int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20the%20maximum%20number%20to%20know%20number%20of%20digits%0A%20%20%20%20%20%20%20%20int%20m%20%3D%20getMax(arr%2C%20n)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Do%20counting%20sort%20for%20every%20digit.%20Note%20that%20instead%0A%20%20%20%20%20%20%20%20%2F%2F%20of%20passing%20digit%20number%2C%20exp%20is%20passed.%20exp%20is%2010%5Ei%0A%20%20%20%20%20%20%20%20%2F%2F%20where%20i%20is%20current%20digit%20number%0A%20%20%20%20%20%20%20%20for%20(int%20exp%20%3D%201%3B%20m%2Fexp%20%3E%200%3B%20exp%20*%3D%2010)%0A%20%20%20%20%20%20%20%20%20%20%20%20countSort(arr%2C%20n%2C%20exp)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20function%20to%20print%20an%20array%0A%20%20%20%20static%20void%20print(int%20arr%5B%5D%2C%20int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%2B%22%20%22)%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%20%2F*Driver%20function%20to%20check%20for%20above%20function*%2F%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B170%2C%2045%2C%2075%2C%2090%2C%20802%2C%2024%2C%202%2C%2066%7D%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20radixsort(arr%2C%20n)%3B%0A%20%20%20%20%20%20%20%20print(arr%2C%20n)%3B%0A%20%20%20%20%7D%0A%7D” message=”java” highlight=”” provider=”manual”/]

PYTHON

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20implementation%20of%20Radix%20Sort%0A%20%0A%23%20A%20function%20to%20do%20counting%20sort%20of%20arr%5B%5D%20according%20to%0A%23%20the%20digit%20represented%20by%20exp.%0Adef%20countingSort(arr%2C%20exp1)%3A%0A%20%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%0A%20%20%20%20%23%20The%20output%20array%20elements%20that%20will%20have%20sorted%20arr%0A%20%20%20%20output%20%3D%20%5B0%5D%20*%20(n)%0A%20%0A%20%20%20%20%23%20initialize%20count%20array%20as%200%0A%20%20%20%20count%20%3D%20%5B0%5D%20*%20(10)%0A%20%0A%20%20%20%20%23%20Store%20count%20of%20occurrences%20in%20count%5B%5D%0A%20%20%20%20for%20i%20in%20range(0%2C%20n)%3A%0A%20%20%20%20%20%20%20%20index%20%3D%20(arr%5Bi%5D%2Fexp1)%0A%20%20%20%20%20%20%20%20count%5B%20(index)%2510%20%5D%20%2B%3D%201%0A%20%0A%20%20%20%20%23%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%23%20%20position%20of%20this%20digit%20in%20output%20array%0A%20%20%20%20for%20i%20in%20range(1%2C10)%3A%0A%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi-1%5D%0A%20%0A%20%20%20%20%23%20Build%20the%20output%20array%0A%20%20%20%20i%20%3D%20n-1%0A%20%20%20%20while%20i%3E%3D0%3A%0A%20%20%20%20%20%20%20%20index%20%3D%20(arr%5Bi%5D%2Fexp1)%0A%20%20%20%20%20%20%20%20output%5B%20count%5B%20(index)%2510%20%5D%20-%201%5D%20%3D%20arr%5Bi%5D%0A%20%20%20%20%20%20%20%20count%5B%20(index)%2510%20%5D%20-%3D%201%0A%20%20%20%20%20%20%20%20i%20-%3D%201%0A%20%0A%20%20%20%20%23%20Copying%20the%20output%20array%20to%20arr%5B%5D%2C%0A%20%20%20%20%23%20so%20that%20arr%20now%20contains%20sorted%20numbers%0A%20%20%20%20i%20%3D%200%0A%20%20%20%20for%20i%20in%20range(0%2Clen(arr))%3A%0A%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20output%5Bi%5D%0A%20%0A%23%20Method%20to%20do%20Radix%20Sort%0Adef%20radixSort(arr)%3A%0A%20%0A%20%20%20%20%23%20Find%20the%20maximum%20number%20to%20know%20number%20of%20digits%0A%20%20%20%20max1%20%3D%20max(arr)%0A%20%0A%20%20%20%20%23%20Do%20counting%20sort%20for%20every%20digit.%20Note%20that%20instead%0A%20%20%20%20%23%20of%20passing%20digit%20number%2C%20exp%20is%20passed.%20exp%20is%2010%5Ei%0A%20%20%20%20%23%20where%20i%20is%20current%20digit%20number%0A%20%20%20%20exp%20%3D%201%0A%20%20%20%20while%20max1%2Fexp%20%3E%200%3A%0A%20%20%20%20%20%20%20%20countingSort(arr%2Cexp)%0A%20%20%20%20%20%20%20%20exp%20*%3D%2010%0A%20%0A%23%20Driver%20code%20to%20test%20above%0Aarr%20%3D%20%5B%20170%2C%2045%2C%2075%2C%2090%2C%20802%2C%2024%2C%202%2C%2066%5D%0AradixSort(arr)%0A%20%0Afor%20i%20in%20range(len(arr))%3A%0A%20%20%20%20print(arr%5Bi%5D)%2C” message=”PYTHON” highlight=”” provider=”manual”/]

Output:

2 24 45 66 75 90 170 802
[ad type=”banner”]