|
| 1 | +# Radix Sort |
| 2 | + |
| 3 | +**Radix sort** is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. 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](../Counting%20Sort/) as a subroutine to sort. |
| 4 | + |
| 5 | +#### Idea: |
| 6 | + |
| 7 | + |
| 8 | +Assume the input array is: |
| 9 | +123, 2, 999, 609, 111 |
| 10 | + |
| 11 | +Based on the algorithm, we will sort the input array according to the one's digit (least significant digit). |
| 12 | + |
| 13 | +``` |
| 14 | +[0]: |
| 15 | +[1]: 111 |
| 16 | +[2]: 2 |
| 17 | +[3]: 123 |
| 18 | +[4]: |
| 19 | +[5]: |
| 20 | +[6]: |
| 21 | +[7]: |
| 22 | +[8]: |
| 23 | +[9]: 999, 609 |
| 24 | +``` |
| 25 | + |
| 26 | +So, the array becomes 111, 2, 123, 999, 609. Note that we placed 999 before 609 because it appeared first. |
| 27 | + |
| 28 | +Now, we'll sort according to the ten's digit: |
| 29 | + |
| 30 | +``` |
| 31 | +[0]: 609, 2 |
| 32 | +[1]: 111 |
| 33 | +[2]: 123 |
| 34 | +[3]: |
| 35 | +[4]: |
| 36 | +[5]: |
| 37 | +[6]: |
| 38 | +[7]: |
| 39 | +[8]: |
| 40 | +[9]: 999 |
| 41 | +``` |
| 42 | + |
| 43 | +Now, the array becomes : 609, 2, 111, 123, 999 |
| 44 | + |
| 45 | +Finally , we sort according to the hundred's digit (most significant digit): |
| 46 | + |
| 47 | +``` |
| 48 | +[0]: 2 |
| 49 | +[1]: 111, 123 |
| 50 | +[2]: |
| 51 | +[3]: |
| 52 | +[4]: |
| 53 | +[5]: |
| 54 | +[6]: 609 |
| 55 | +[7]: |
| 56 | +[8]: |
| 57 | +[9]: 999 |
| 58 | +``` |
| 59 | + |
| 60 | +The array becomes : 2, 111, 123, 609, 999 which is sorted. This is how Radix Sort algorithm works. |
| 61 | + |
| 62 | + |
| 63 | +#### Video Tutorial |
| 64 | +<a href="http://www.youtube.com/watch?feature=player_embedded&v=YXFI4osELGU" target="_blank"><img src="http://img.youtube.com/vi/YXFI4osELGU/0.jpg" |
| 65 | +alt="Counting Sort (youtube)" width="640" height="480" border="10" /></a> |
| 66 | + |
| 67 | + |
| 68 | +#### Complexity Analysis |
| 69 | +The time complexity for radix sort is : O(d(n+b)), where b is the base for representing numbers, for example, for decimal system, b is 10. If k is the maximum possible value, then d would be O(log<sub>b</sub>(k)). So overall time complexity is O((n+b) * logb(k)) |
| 70 | + |
| 71 | +### More on this topic |
| 72 | +- [Radix Sort - WikiPedia](https://en.wikipedia.org/wiki/Radix_sort) |
| 73 | +- [Radix Sort - HackerEarth Tutorial](https://www.hackerearth.com/practice/algorithms/sorting/radix-sort/tutorial/) |
| 74 | +- [Radix Sort - geeksforgeeks](http://www.geeksforgeeks.org/radix-sort/) |
0 commit comments