Sorting II
Trace LSD radix sort, apply the decision-tree lower bound, reason about stability, and exam-level questions on when comparison sorts can be beaten.
- Lessons
- The limits of sorting · Radix sorts
This discussion goes with Lessons 56 and 57.
Q1: What would Swift print?
What would Swift print?
var r = [170, 45, 75, 90, 802, 24, 2, 66]
for d in [1, 10] {
r = (0...9).flatMap { digit in r.filter { ($0 / d) % 10 == digit } }
}
print(r)Show answer and explanation
Answer[802, 2, 24, 45, 66, 170, 75, 90]
After the pass on the last digit: 170, 90, 802, 2, 24, 45, 75, 66. The stable pass on the
tens digit then groups 802 and 2 (tens digit 0), 24, 45, 66, 170 and 75 (both 7), and 90.
The last two digits are now sorted; one more pass, on the hundreds, finishes the job.
Each filter keeps the current order, which is what makes every pass stable.
Q2: The lower bound
- What is the minimum number of comparisons, in the worst case, to sort 5 items?
- A student claims a comparison sort that sorts any 8 items with 15 comparisons. Is it possible?
- Does the Ω(N log N) bound apply to finding the maximum of N items?
Show solution
- ⌈log₂ 5!⌉ = ⌈log₂ 120⌉ = 7, and 7 comparisons are known to suffice.
- No. 8! = 40,320 orders need a decision tree with at least that many leaves, and a tree of height 15 has at most 2¹⁵ = 32,768. At least 16 comparisons are needed.
- No. The maximum is one of only N answers, so the argument gives just log₂ N; in fact N − 1 comparisons are needed and suffice, since every item but the maximum must lose at least one comparison.
Q3: Stability matters
A list of students is sorted by name. Then it is sorted by grade. With a stable second sort, what order are students with the same grade in? And with an unstable one? Show how LSD radix sort relies on the same idea.
Show solution
With a stable sort by grade, students with equal grades stay in name order, so the result is sorted by grade, and by name within each grade. With an unstable sort, equal grades can come out in any order.
LSD radix sort is exactly this, repeated: sort by the least important key (the last digit), then stably by the next one, and so on. Each stable pass preserves the order the earlier passes established among equal digits.
struct Student { let name: String; let grade: Int }
let students = [Student(name: "Bo", grade: 90), Student(name: "Ada", grade: 85),
Student(name: "Cy", grade: 90), Student(name: "Di", grade: 85)]
let byName = students.sorted { $0.name < $1.name }
let byGradeThenName = byName.sorted { $0.grade > $1.grade } // Swift’s sort is stable
assert(byGradeThenName.map(\.name) == ["Bo", "Cy", "Ada", "Di"])Q4: Exam-level: beating N log N
For each task, say whether it can be done in O(N) time, and how.
- Sort N integers, each between 0 and 1,000.
- Sort N arbitrary strings, using only a comparator.
- Sort N 64-bit integers.
- Decide whether N floating-point numbers contain a duplicate, using only comparisons.
Show solution
- Yes: counting sort, Θ(N + 1,001).
- No: with only a comparator, Ω(N log N) comparisons are necessary.
- Yes: LSD radix sort with 8 passes over bytes is Θ(8(N + 256)) = Θ(N), for a fixed key width.
- No: element distinctness needs Ω(N log N) comparisons. With hashing, which is not comparing, it takes expected Θ(N).