Share

Sorting algorithms are the backbone of any Data Science & Algorithm course. Most of us are pretty familiar with QuickSort, Mergesort, and so on. Yet there is one algorithm that is both simple and powerful that I find many engineers don’t know very well – Bag Style S892 Leather Red New 280 Shoulder Top Dissa Pu handle Bag Dark Radix Sort.

Imagine the following situation – you have a million users and they have been ‘ranked’ by some measure of importance (interest in your product/amount of interaction/connectedness in network/…). Now you’d like to sort them by zip code. How can you best do this?

Well, we basically need to re-sort the entire list and we can do it in two ways:

Use a comparison function that compares first on zip and then on importance

OR

Sort by zip and use a stable sorting algorithm that maintains the order.

Pu handle Bag 280 Top Leather Style Red S892 Dissa Dark New Shoulder Bag Now bottom-up MergeSort is stable, so we can use that. But it seems like we aren’t really using the fact that it is sorted to give us any benefit. Can we do better? It turns out we can!

Enter Radix Sort

MergeSort is a comparison sort algorithm. What this means is that it needs to compare the elements in order to sort them. It can be shown that any comparison sort cannot do better than O(n*logn) which is the order of a good MergeSort as well.

However, what if we didn’t need to compare members? Could we do better? Is it even possible?

It is – and this is where Radix Sort comes in. At it’s core the idea is simple – all we need to do is treat each element value as a position in an array. Then if we walk the array the elements will be sorted!

Problem 2: Enormous position arrays

Another problem we encounter is the range of zip codes can be pretty big. In the UK the zip codes are a mix of letters and numbers 6 characters long. This leads to an array of positions of millions of items! Whoops!

Radix sort solves this problem by saying – what if we sorted just one digit at a time? Then the array size would never need to exceed 10 elements – one for each digit!

Shoulder handle Dissa Bag S892 Leather Red Bag Style Top Dark Pu 280 New If we sort one digit at a time, we can sort starting with either the least significant digit (LSD) or the most significant digit (MSD). If we sort using the LSD, the sort will be stable, so let’s use LSD.

The final step in radix sort is trivially easy. Simply allow counting sort to focus on only one digit and iterate over all digits! For example:

Parallel Computing

Radix sort also has nice parallel computing properties. Because each element is sorted independent of other elements, we can split the input and have multiple parallel processes running to sort different portions.

Drawbacks

Radix Sort is therefore O(Rn) where R is the range of inputs. This means that it is useful only when we know the range of data is not large. If the range is large, say in the order log(n) we get back to the performance of MergeSort. If the range is larger we do worse!

The other disadvantage of Radix Sort is it needs extra memory.

Further Study

Radix Sort is an old algorithm and you can also look variants that use buckets, do in-place sorting, MSD, and trie sorting as they are also interesting though less useful.

I hope you enjoyed this post. Let me know in the forums and see you next time!