[Algorithm] Radix Sort Algorithm

时间:2024-04-15 08:40:42

For example we have the array like this:

[, , , , , ]

First step is using Counting sort for last digit, in our example is:

[53, 89, 150, 36, 633, 233]
[, , , , , ]

Then sort according to the last digit:

[, , , , , ]

Then using second last digit to the sort:

[, , , , , ]

Last using the last digist to do the sort, and using '0' if missing the digit:

[, , , , , ]

The whole array should be sorted.

Time complexiity:

n = 6: array length is 6

d = 3: max digit for number is 3

b = 10, we use 10 based counting [0, 1,2,3...9]

For each step it takes O(n+b) to do the sorting

Then for the whole algorithm is O(d * (n+b))