Skip to contentKEEP THE CRAFTTHE SANCTUARY

Sort Nonnegative Integers by Digits

Sort nonnegative integers one decimal digit at a time, from the ones digit up, with ten stable buckets per pass.

The problem

Implement radix_sort(values). values is a list of nonnegative integers, possibly with duplicates. Return a new list with the same values in ascending order. Do not call sorted or list.sort: sort the numbers digit by digit, starting from the ones digit. For example, radix_sort([170, 45, 75, 90, 802, 24, 2, 66]) returns [2, 24, 45, 66, 75, 90, 170, 802].

Starter code

def radix_sort(values):
    pass

Public examples

  1. values = [170, 45, 75, 90, 802, 24, 2, 66]gives[2, 24, 45, 66, 75, 90, 170, 802]
  2. values = [0, 10, 0, 1]gives[0, 0, 1, 10]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.