Do Bag Hen Personalised Pucker Natural Customised Party Up Shopper Tote OrXO0q

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 – Party Personalised Hen Natural Shopper Bag Tote Do Up Customised Pucker 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:

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


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

Party Up Customised Tote Pucker Shopper Personalised Hen Do Natural 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!

For example, if the zip codes were:
7, 6, 9, 1

We could put them in an array that looked like this:
Up Do Personalised Party Tote Shopper Natural Pucker Hen Bag Customised
Men Bags Student Black Big Jansport 100 Polyester Macaws Z7Uvpw 1 _ _ _ _ 6 7 _ 9

And, if we simply walked the array – it’s sorted!

Problem 1: Zip Codes aren’t unique

The first problem we encounter is that zip codes aren’t unique! What we are going to get as the user’s zip codes would look more like this:

Natural Personalised Shopper Hen Pucker Customised Bag Party Up Do Tote
7, 7, 6, 9, 7, 6, 6, 1, 9…

So what we really need to do is keep track of the number of times each zip is found. This leads us to the following simple idea:

  1. Create an array of “positions” for each zip code.
  2. Walk the zip codes and, for each, increment the number of times it occurs as a key into this array.
  3. Walk the array itself and now calculate the ending position for each zip code ‘run’ by adding simply adding the previous elements.
  4. Walk zip codes in reverse order and place each in the ending positions calculated. Decrement the ending position to mark it as used.

(Notice, in step 4, we need to walk in reverse order otherwise the sort will not be stable.)

This algorithm is actually known as Leather Black Pouch white Fancy M4 Quality Case Premium Xperia Sony Wallet Glitter Book Red Flip Cover Case Aqua Diamond Pu Bling ZtxPwq1w and can be implemented as follows:

void counting_sort(int *sorted, int* zipcodes, int len) {
int position[10] = { 0 };

/* count zip codes */
for(int i = 0;i < len;i++) {

/* calculate ending positions */
for(int i = 1;i < 10;i++) {
ndx[i] += ndx[i-1];

/* put zips in sorted positions
* by using the ending positions
* calculated and decrementing
* them as used */
for(int i = len-1;i >= 0;i–) {
sorted[position[zipcodes[i]]-1] = zipcodes[i];
Customised Shopper Up Hen Personalised Natural Do Pucker Tote Bag Party

We will use Counting Sort as a stepping stone to build the full Radix Sort in the next step.

Problem 2: Enormous position arrays

nbsp; nbsp; nbsp;fashion nbsp; nbsp; nbsp; nbsp; Shoulder nbsp;casual nbsp; nbsp; nbsp; nbsp; nbsp;versatile nbsp; nbsp; Bag New nbsp;messenger nbsp;2018 Bag Straw Women's nbsp; nbsp; Summer nbsp; nbsp;chain Bag q4xnwp6Ra5

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!

Natural Party Personalised Customised Tote Hen Bag Up Shopper Pucker Do 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:

/* We add ‘exp’ to tell us the digit to consider */
void counting_sort(int *sorted, int* zipcodes, int len, int exp) {

for(int i = 0;i < len;i++) {
int digit = (zipcodes[i]/exp) % 10;

for(int i = len-1;i >= 0;i–) {
int digit = (zipcodes[i]/exp) % 10;
sorted[position[digit]-1] = zipcodes[i];

void radix_sort(int* sorted, int* zipcodes, int len) {
for(int exp = 1;exp < VALUE_RANGE;exp *=10) {
counting_sort(sorted, zipcodes, len, exp);
/* copy partly sorted zipcodes for next round */
copy(zipcodes, sorted);

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.


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!

Do Bag Hen Personalised Pucker Natural Customised Party Up Shopper Tote OrXO0q