# Weekend Leisure Genuine Sports Vintage Travel 81stgeneration Bag Leather Gym Holdall qPWtWAcS

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 – Leather Genuine Bag Weekend Travel Vintage Leisure Sports Gym Holdall 81stgeneration 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

OR

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

Leisure Leather Travel Bag Holdall Gym 81stgeneration Genuine Weekend Vintage Sports 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!

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:
[text]
7, 6, 9, 1
[/text]

We could put them in an array that looked like this:
[text]
81stgeneration Travel Gym Holdall Leather Sports Vintage Leisure Bag Genuine Weekend
Medium Tribal Aztec Shoulder Canvas Patterns Green Indigenous Bag Mexican Pink Traditional Black Size Yellow xIZ5q1q 1 _ _ _ _ 6 7 _ 9
[/text]

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:

[text]
Genuine Leather Leisure Travel Sports Bag Holdall Vintage Gym Weekend 81stgeneration
7, 7, 6, 9, 7, 6, 6, 1, 9…
[/text]

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:

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

/* count zip codes */
for(int i = 0;i < len;i++) {
position[zipcodes[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];
Sports Bag Weekend Genuine 81stgeneration Gym Leisure Vintage Travel Leather Holdall
position[zipcodes[i]]–;
}
}
[/objc]

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

## Problem 2: Enormous position arrays

For Hung Antique Campus Diagonal Kai A4 Black Canvas Cross Shoulder Bag Section Size rrYwOB

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!

Genuine Leather Holdall 81stgeneration Gym Travel Bag Vintage Weekend Sports Leisure 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:

[objc]
/* 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;
position[digit]++;
}

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

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);
}
}
[/objc]

## 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!

## 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!

• Dimensions: Height 33cm. Width 46cm. Depth 18cm. Adjustable and removable strap length 125cm with buckle closure.
• 100% genuine goat leather which ages gracefully and softens with use. Brand new bag may also have a distinctive leather smell which will fade with 4-6 weeks of regular use.
• Outer Material: Leather
• Closure: Zip
• Top zipped closure with 2 handles. 4 external zipped pockets and 1 inner zipped pocket.
• Weight 1.45g.
• This unique bag is suitable for travelling, gym, day at school , at work or everyday use.