Skip to main content

Radix Sort Primer

When you pick a random developer and you ask him, hey, what is the time complexity lower bound for a search algorithm, the answer, which has become today almost a reflex is O(nlgn) which is also called linearithmic. 

If you search Wikipedia for the running time of radix sort it's saying hold to your chair, O(n), how could that be you ask? It makes a few assumptions and does it more efficiently, it's trick is that the look and peek into the keys themself s, and then it manages to be a non comparison based algorithm.  So while it's true that the lower bound of search algorithms which are based on comparisons is O(nlgn) for non comparison based (such as radix sort) it's O(n)

If you have the array already sorted we could say that our running time for sorting the array is O(1), so aren't we tricking reality, the difference is that the O(n) on radix sort holds also for a non-sorted array.

Examples of sorts which are comparison based sorting algorithms are all the usual ones like quick sort, merge sort, heap sort, bubble sort, insertion sort.

Here is a very naive intuition behind how you can do sorting without comparing anything.

Let's say that you have a random list of numbers, let's say we generated a random list of numbers in between the range 0 to 1000 .  Now as they are random they are unordered, and we would like to order them.

How would you do this without any comparisons?  One way to do this is to create a new array.  Now we are going to scan each of the randomly generated numbers and use the value of the number to insert it into this new array.

So if our random numbers were

101, 202, 50, 900

Then our new array could look like this:

A[0] = 0
A[1] = 0
A[50] = 1
A[101] = 1
A[202] = 1
A[900] = 1
A[1000] = 0

Note that we could put any value inside those array cells.  It's very convenient however to just put 1 because if we could have duplicates then we could increase this number to 2.

Next in order to get the sorted array we would simply scan this array and print the number the index of the array every time we see 1, and if we see 2 we print this number twice.

This looks great and this is O(n) and this is not even radix sort, this is called counting sort, why would we then even need radix sort?

The answer is that we needed in the new array as many cells as the range of numbers ( 0 - 1000) we had in our original list.  So we could find the minimal number then the maximal one and then create an array of that range.

But what if our array to sort is so huge, meaning if this was not just a list of numbers from 0 to 1000 but lets say credit card numbers or social security numbers then our range would be really large so you might find that you need to create an array of size 1G or even more without special optimization.

Radix sort optimizes this so that the array that you need to create its size the space that it's going to take is simply, according to the range of digits, so if your digits are from 0 to 9 it doesn't matter how many numbers we have what is the range we simply need an array of size 10 to accommodate all the digits.

This so cool because we get to enjoy all words both a fax O(n) sort and also low on space consumption.

Now how does it do that! We create a new array like we created before but now each cell is simply the digits. So we have this array now.,

A[0] =
A[1] =
A[9] =

Now what do we do with this array, we scan the numbers and for each scan we are going to do to start with first digit and then put all the numbers that have this digit at this place on the array.  So if the first digit from right LSD (The Least Significant Digit) we do LSD because we want the highest significant digit to have the last word because it is the most important so it would be the one to actually say the last word and who comes after whom.

So we take the first digit scan each number lets say we see 3 numbers that end with 0, we put all those numbers in A[0].  We see 2 numbers that end with [3] so we put these numbers in cell number [3].

So if we had 202, 153, 103 to sort out first scan which looks at the LSD would do

A[2] = 202
A[3] = 153, 103

Now we go to the middle digit but now we don't scan the original numbers we scan the numbers according to their order in the helper array that we have just build, if we don't do that we lose all the sorting that we did with this last digit! and again we put the items in our array

A[0] = 202, 103
A[5] = 153

Now we go to the most significant digit and we get

A[1] = 103
A[2] = 202

Now we get to the A[5] = 153, so we put it in A[1] note that when we put it in put it as the last item on A[1] which turns it out to be sorted this is because we already sorted by the previous digits. so hence we get:

A[1] = 103, 153
A[2] = 202

And now we if scan our helper array we get: 103, 153, 202 which is a sorted array!

There are a few other ways to do this, such as storing exactly the output indexes of the radixly sorted array, but we won't do this now.


Popular posts from this blog

Dev OnCall Patterns

Introduction Being On-Call is not easy. So does writing software. Being On-Call is not just a magic solution, anyone who has been On-Call can tell you that, it's a stressful, you could be woken up at the middle of the night, and be undress stress, there are way's to mitigate that. White having software developers as On-Calls has its benefits, in order to preserve the benefits you should take special measurements in order to mitigate the stress and lack of sleep missing work-life balance that comes along with it. Many software developers can tell you that even if they were not being contacted the thought of being available 24/7 had its toll on them. But on the contrary a software developer who is an On-Call's gains many insights into troubleshooting, responsibility and deeper understanding of the code that he and his peers wrote. Being an On-Call all has become a natural part of software development. Please note I do not call software development software engineering b

Containers - Quick Low Level Guide

Containers Kernel, namespace, cgroups Kernel space and user space Before we actually get to explain containers let's define what is a kernel.  Because you know there is no such thing in reality as a kernel it's only how we name things, and different people name things differently. cgroups, namespaces, UFS We are going to discuss containers, cgroups, namespace, UFS, hypervisor, user space, kernetl space and more.   When we say "kernel" we mean this.  We have the hardware, this is not the kernel, now above the hardware we have a few layers of software, imagine now two boxes. User mode is all the application you run while the kernel is the lower level is all the virtual memory management scheduling, connection to hardware devices, network drivers, it's basically the abstraction on top of the hardware + the basic services which allow this. One box is closer to the hardware and contains a few layers, the second box sits on top of the kernel box and contains

Recursion Trees Primer

Recursion trees. Controlling the fundamentals stands at the cornerstone of controlling a topic.  In our case in order to be a good developer its not enough or even not at all important to control the latest Java/JavaScript/big data technology but what's really important is the basics.  And the basics in computer science are maths, stats, algorithms and computer structure. Steve wosniak the co-founder of apple said the same, what gave him his relative advantage was his deep understanding of programming and computer structure, this is what gave him the ability to create computer's which are less costly than the competitors (not that there were many) and by the way there were 3 founders to apple company one responsible for the technical side, one for the product and sales (Steve Jobs) and the third responsible for the company structure and growth, each of the three extremely important, it was not only the two Steve's but that's a topic for another episode. And with t