How would you sort 10 million phone numbers?
MediumSorting
Technical
Content review
Report an errorNot yet reviewed
Show model answer
Phone numbers are fixed-length digit strings, so comparison sorting is not the best tool. LSD radix sort over the digits (or over larger chunks such as 16-bit pieces) sorts them in O(d · n) time with a few linear passes, typically faster than an O(n log n) sort with ten million keys.
If the numbers are distinct and memory is tight, a bit vector indexed by the number works: set the bit for each number, then scan the bits in order. Seven-digit numbers need only 10⁷ bits (about 1.25 MB); ten-digit numbers need 10¹⁰ bits (about 1.25 GB), so you might partition by prefix and process one range at a time. With duplicates, use small counters instead of bits.