Learn
Radix Sort: Python
Bucketing Numbers

The least significant digit radix sort algorithm takes each number in the input list, looks at the digits of that number in order from right to left, and incrementally stuffs each number into the bucket corresponding to the value of that digit.

First we’re going to write this logic for the least significant digit, then we’re going to loop over the code we write to do that for every digit.

Instructions

1.

We’ll need to iterate over being_sorted. Grab each value of being_sorted and save it as the temporary variable number.

2.

Now convert number to a string and save that as number_as_a_string.

3.

How do we get the last element of a string? This would correspond to the least significant digit of the number. For strings, this is simple, we can use a negative index.

Save the last element of number_as_a_string to the variable digit.

4.

Now that we have a string containing the least significant digit of number saved to the variable digit. We want to use digit as a list index for digits. Unfortunately, it needs to be an integer to do that. But that should be easy for us to do:

Set digit equal to the integer form of digit.

5.

We know that digits[digit] is an empty list (because digits has ten lists and digit is a number from 0 to 9). So let’s add our number to that list!

Call .append() on digits[digit] with the argument number.

6.

Now break out of the for loop and return digits.

Folder Icon

Sign up to start coding

Already have an account?