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

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.

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 arrayA[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

## Post a Comment