Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 01 · DSA · The toolbox · about 18 min to read

Play it as a journeyTutor comes with a plan

Beating the n log n wall: counting and radix sort

Sorting and Searching
Sorting and Searching · Open board

A pile of numbers split around a pivot and put back in order. This lesson sorts the same kind of pile without ever comparing two numbers.

A state board has published the exam results for 1,000,000 students. Every mark is a whole number from 0 to 100. The board needs a rank list, highest mark first, and it needs it in seconds. The usual sort works, but it does more work than needed. Marks are small numbers you can count.

By the end you will be able to sort numbers in a single pass or a few passes with no comparisons, say when that beats the library sort, and say when it loses.

The history (optional)

Herman Hollerith’s tabulating system for the 1890 US census had a sorting box: an oak box with 26 compartments, and a lid opened for the compartment where a card belonged (Smithsonian, National Museum of American History). Radix sorting dates back to his work in 1887. By 1923 it was in common use for sorting punched cards (Wikipedia, “Radix sort”). In 1954 Harold H. Seward, in a master’s thesis at MIT, gave counting sort and the first memory-efficient computer version of radix sort. His idea was one scan to learn how big each bucket must be, so the memory could be set aside once.

Before you start. Sorting & Searching gave you quicksort, merge sort and the argument that no sort which only compares items can beat about n log n comparisons. Complexity & Big-O gave you the idea that work grows with input size, and that log n grows very slowly. Hashing gave you the idea of using a value as a position in an array. This lesson uses that last idea to avoid comparing.

Lesson 10 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Comparison sort: why there is a floor
  • Counting sort: count the values instead of comparing them
  • Stable counting: keeping the names with the marks
  • Radix sort: sort one digit at a time
  • Bucket sort: spread, then sort each small bucket
  • When each one wins

Terms it explains: comparison sort, counting sort, stable, Radix sort, Bucket sort, Common mistakes, binary search, binary-search-on-the-answer, counting/radix/bucket, external sort, heapsort, in-place, invariant, merge sort, pivot/partition, quicksort, stable sort, the O(n log n) wall, Timsort/introsort.

Review DSA