Link

Radix Sorts Reading

Complete the Reading Quiz by noon before lecture.

So far the sorting algorithms we have learned in this class rely on comparisons to differentiate each value. While we’ve discussed some optimizations, it’s still true that the lower bound runtime for comparison-based sorting of random values is order N log N. Can we do better than this? The answer is yes, if we use a different approach!

Counting Sort

Assume we’re given this list of (wonderful) TAs with corresponding keys. We are guaranteed the keys are unique and include all values 0 - N-1. Our goal is to sort the rows by the keys in ascending order.

IndexKeyTA
03Lucy
12Thanika
25Louis
30Velocity
41Brian
54Elena
66Howard

Because we are guaranteed that the keys are unique and 0–6, we know that our keys in the output will be in order 0, 1, 2, 3, 4, 5, 6. Given that we know the index each key should be placed, we can walk through the rows in order and place them in the correct index in the output. This is radically different from the sorting algorithms we’ve seen before! When we see a row, we know exactly its correct index in the output.

After moving Lucy, Thanika, and Louis (remember we go through the given data in order), our auxiliary output will look like this:

IndexKeyTA
00 (inferred)?? (not filled yet)
11 (inferred)?? (not filled yet)
22Thanika
33Lucy
44 (inferred)?? (not filled yet)
55Louis
66 (inferred)?? (not filled yet)

Note that this is a simplified algorithm for counting sort because we were guaranteed to have unique keys, and that the keys would be the values 0–N-1. Let’s look at a slightly different problem where we don’t have these guarantees.

Each TA in the list now has a card from a standard deck. Our new goal is to sort them by the card suit, in the order {♣, ♠, , }. You can think of clubs ♣ as the smallest and diamonds as the largest. All TAs within a given suit are considered equal, and we want the sort to be stable. Remember a sort is stable if values with equal keys are in the same order in the output as they were in the input.

IndexSuit (Key)TA
0Lucy
1Thanika
2Louis
3Velocity
4Brian
5Elena
6Howard
What will be the index of the first heart ? How do we know that?

The cards that come before are ♣ and ♠, so we can count the number of cards with suit ♣ or ♠ to determine the index of the first . In this case, there are four cards of suit ♣ or ♠, so the first will be at index 4. Using this type of analysis to determine the starting index of each group, we can generalize counting sort to work for non-unique, non-consecutive, and non-numeric keys! We’ll continue to explore this in lecture.

We will use counting sort as the foundation for radix sorts. Radix is the number of different digits or characters in a given alphabet. For example, the radix for card suits from a standard deck is 4 because there are four options {♣, ♠, , }.


Reading Quiz