bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/DSA/Time Complexity
DSA•Time Complexity

DSA Radix Sort Time Complexity

Native lesson simulator

Radix sort

DSA Radix Sort Time Complexity

Radix sort17004517529038024

Radix sort processes one digit place at a time.

Radix Sort Time Complexity

The Radix Sort algorithm sorts non negative integers, one digit at a time.

There are n values that need to be sorted, and k is the number of digits in the highest value.

When Radix Sort runs, every value is moved to the radix array, and then every value is moved back into the initial array. So n values are moved into the radix array, and n values are moved back. This gives us n + n=2 · n operations.

And the moving of values as described above needs to be done for every digit. This gives us a total of 2 · n · k operations.

This gives us time complexity for Radix Sort:

O(2 · n · k) = O(n · k)

Radix Sort is perhaps the fastest sorting algorithms there is, as long as the number of digits k is kept relatively small compared to the number of values n.

We can imagine a scenario where the number of digits k is the same as the number of values n, in such a case we get time complexity O(n · k)=O(n^2) which is quite slow, and has the same time complexity as for example Bubble Sort.

We can also image a scenario where the number of digits k grow as the number of values n grow, so that k(n)= log n. In such a scenario we get time complexity O(n · k)=O(n · log n ), which is the same as for example Quicksort.

See the time complexity for Radix Sort in the image below.

Radix Sort Simulation

Run different simulations of Radix Sort to see how the number of operations falls between the worst case scenario O(n^2) (red line) and best case scenario O(n) (green line).

The bars representing the different values are scaled to fit the window, so that it looks ok. This means that values with 7 digits look like they are just 5 times bigger than values with 2 digits, but in reality, values with 7 digits are actually 5000 times bigger than values with 2 digits!

If we hold n and k fixed, the "Random", "Descending" and "Ascending" alternatives in the simulation above results in the same number of operations. This is because the same thing happens in all three cases.

Previous

DSA Counting Sort Time Complexity

Next

DSA Merge Sort Time Complexity

This chapter

Overview
10

Lessons

36m

Read time

1. DSA Time Complexity2. DSA Bubble Sort Time Complexity3. DSA Selection Sort Time Complexity4. DSA Insertion Sort Time Complexity5. DSA Quicksort Time Complexity6. DSA Counting Sort Time Complexity7. DSA Radix Sort Time Complexity8. DSA Merge Sort Time Complexity9. DSA Linear Search Time Complexity10. DSA Binary Search Time Complexity

On this page

Radix Sort Time ComplexityRadix Sort Simulation