Radix Sorts Study Guide
Radix. Just another word for base, as in the base of a number system. For example, the radix for words written in lowercase English letters is 26. Numbers written in Arabic numerals have a radix of 10.
Radix sort. A sort that works with one character at a time by grouping objects that have the same digit in the same position.
Counting sort. Allows you to sort N keys that are integers between 0 and R-1 in order N + R time. Beats linearithmic lower bound by avoiding any binary compares. This is a completely different philosophy for how things should be sorted.
LSD radix sort. In the LSD radix sort algorithm, we sort by each digit, working from right to left. Requires examination of WN digits, where W is the length of the longest key. Runtime is in order WN + WR, though we usually think of R as a constant and just say WN. The WR part of the runtime is due to the creation of length R arrays for counting sort. We usually do LSD radix sort using counting sort as a subroutine, but it’s worth thinking about whether other sorts might work as well.
Counting sorts vs. comparison sorts. Our comparison sorts, despite requiring order N log N time, can still be faster than LSD radix sort, which is linear in the length of our input WN. For extremely large N, LSD radix sort will naturally win, but log N is typically pretty small. Know which algorithm is best in the two extreme cases of very long dissimilar strings and very long, nearly equal strings.
MSD radix sort. In MSD radix sort, we work from left to right, and solve each resulting subproblem independently. Thus, for each problem, we may have as many as R subproblem. Worst case runtime is exactly the same as LSD sort, WN + WR, though can be much better. In the very best case, where we only have to look at the top character (only possible for R > N), we have a runtime of N + R.
Recommended Problems
- For a fixed alphabet and key size, what are the best case and worst case inputs for LSD? For MSD?
- [Adapted from Textbook 5.1.8] Give the number of characters examined by MSD string sort for a file of N keys a, aa, aaa, aaaa, aaaaa, …
- For very long random strings, would a comparison based sort or a radix sort be faster? For very long, highly similar string, which would be faster?
- For a very large number of 32-bit Java integers, would radix sort or a comparison sort be faster?
- Q12a from CS 61B 17sp Final
- Q8b from CS 61B 18sp Final