Card 10/ 11

ExerciseDifficulty: Advanced3 min

Practice: Choosing a Sort for Three Sensor Logs

Temperatures in whole degrees, 64-bit device ids and readings spread evenly between 0 and 1: pick a sort for each.

Choosing a linear-time sort means reading the keys before reading any code: their type, their range, and how they are spread decide which sort applies, or whether none does. This exercise practises that decision on three logs from the same fleet of weather stations.

Each log holds a million entries, and each has to be sorted by one field.

  1. Temperatures, in whole degrees Celsius. The stations' sensors report from −40 to 60 and nothing outside it.
  2. Device ids, unsigned 64-bit integers assigned at random over their whole range, one per entry.
  3. Readings from a calibration sensor, floating-point fractions from 0 up to but not including 1, which the manufacturer states are spread evenly.

For each log, choose counting sort, radix sort, bucket sort or a comparison sort, and give the property of the keys that decides it. Then estimate the work in counters, passes or buckets.

Show a choice for each log
A sort for each log of a million entries, and why
LognRange or spreadSortWhy
Temperatures1,000,000−40 to 60, so k = 101Counting sortn + k = 1,000,101 steps and 101 counters
Device ids1,000,0000 to 2⁶⁴ − 1Radix sort, 16 bits per digitCounting sort would need 2⁶⁴ counters; 4 passes cost about 4 × (n + 65,536) = 4,262,144 steps
Readings1,000,0000 to 1, spread evenlyBucket sort with n bucketsNot integers, so counting and radix sort do not apply directly; evenly spread keys put O(1) items in each bucket

The temperatures are integers in a range of 101 values against a million items, so k is tiny next to n and counting sort's O(n + k) is O(n). Subtract −40, the smallest key, so the count array starts at it.

The device ids are integers too, but their range is the whole of 64 bits, and counting sort's array of 2⁶⁴ counters, about 1.8 × 10¹⁹, could not be allocated on any machine. Radix sort cuts each id into four 16-bit digits and runs four counting passes of 65,536 counters. Eight passes of 8-bit digits work as well, at about 8,002,048 steps, trading fewer counters for more passes. Either is well below the roughly 19,931,569 comparisons n log₂ n predicts for a comparison sort, though a pass step and a comparison do not cost the same.

The readings are not integers, which rules out indexing by the key itself, but they are spread evenly over a known range, which is the one assumption bucket sort needs. With a million buckets, a reading's bucket is the reading times a million, rounded down. Had the manufacturer not promised an even spread, a comparison sort would be the safe choice, because a cluster of readings would put most of them in a few buckets.

A run sorted a sample of 10,000 entries per log this way, with counting sort for the temperatures, four 16-bit radix passes for the ids and bucket sort for the readings, and each output matched Python's sorted.