Skip to the code
+25 XP

#205 Radix Sort Digit by Digit

medium

A sorting tutorial shows radix sort one digit at a time. Each pass looks at one decimal digit, starting with the ones digit: it goes through the numbers in their current order and puts each into one of ten buckets by that digit, then joins the buckets from 0 to 9. The next pass uses the tens digit, and so on. There is one pass for each digit of the largest number.

Write radixPasses(nums) that returns an array of snapshots: the list after each pass. If the largest number is 0 (or the list is empty), there are no passes, so return [].

Example 1

Input: nums = [170,45,75,90,2,802,24,66]
Output: [[170,90,2,802,24,45,75,66],[2,802,24,45,66,170,75,90],[2,24,45,66,75,90,170,802]]

Three passes, for the ones, tens, and hundreds digits of 802.

Example 2

Input: nums = [3,1,2]
Output: [[1,2,3]]

Every number has one digit, so one pass sorts them.

Constraints

  • 0 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).