Sorting and Algorithm Bounds 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.

  1. For a fixed alphabet and key size, what are the best case and worst case inputs for LSD radix sort and for MSD radix sort?
  2. Q8b from CS 61B 15sp Final (Solution)
  3. Q4a from CS 61B 16sp Final (Solution)
  4. Q10 from CS 61B 16sp Final (Solution)
  5. Q2c, Q2d from CS 61B 17sp Final (Solution)
  6. Q8b from CS 61B 18sp Final (Solution)