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.

Comments

Popular posts from this blog

Functional Programming in Scala for Working Class OOP Java Programmers - Part 1

Introduction Have you ever been to a scala conf and told yourself "I have no idea what this guy talks about?" did you look nervously around and see all people smiling saying "yeah that's obvious " only to get you even more nervous? . If so this post is for you, otherwise just skip it, you already know fp in scala ;) This post is optimistic, although I'm going to say functional programming in scala is not easy, our target is to understand it, so bare with me. Let's face the truth functional programmin in scala is difficult if is difficult if you are just another working class programmer coming mainly from java background. If you came from haskell background then hell it's easy. If you come from heavy math background then hell yes it's easy. But if you are a standard working class java backend engineer with previous OOP design background then hell yeah it's difficult. Scala and Design Patterns An interesting point of view on scala, is

Alternatives to Using UUIDs

  Alternatives to Using UUIDs UUIDs are valuable for several reasons: Global Uniqueness : UUIDs are designed to be globally unique across systems, ensuring that no two identifiers collide unintentionally. This property is crucial for distributed systems, databases, and scenarios where data needs to be uniquely identified regardless of location or time. Standardization : UUIDs adhere to well-defined formats (such as UUIDv4) and are widely supported by various programming languages and platforms. This consistency simplifies interoperability and data exchange. High Collision Resistance : The probability of generating duplicate UUIDs is extremely low due to the combination of timestamp, random bits, and other factors. This collision resistance is essential for avoiding data corruption. However, there are situations where UUIDs may not be the optimal choice: Length and Readability : UUIDs are lengthy (typically 36 characters in their canonical form) and may not be human-readable. In URLs,

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