In computer science, radix sort is a non-comparative sorting algorithm. It avoids comparison by creating and distributing elements into buckets according to their radix. For elements with more than one significant digit, 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. (Source: Wikipedia)
Advantages:
Disadvantages:
(Source: Hackerearth)
radixSort(array)d <- maximum number of digits in the largest elementcreate d buckets of size 0-9for i <- 0 to dsort the elements according to ith place digits using countingSortcountingSort(array, d)max <- find largest element among dth place elementsinitialize count array with all zerosfor j <- 0 to sizefind the total count of each unique digit in dth place of elements andstore the count at jth index in count arrayfor i <- 1 to maxfind the cumulative sum and store it in count array itselffor j <- size down to 1restore the elements to arraydecrease count of each element restored by 1
Name | Best | Average | Worst | Memory | Stable | Comments |
---|---|---|---|---|---|---|
Radix sort | n * k | n * k | n * k | n + k | Yes | k - length of longest key |