Learn

Now that we’ve finished writing our radix sort we’re finished for the day… or are we?

Instructions

1.

Now that we’ve gotten our sort working let’s test it out with some new data.

Run radix_sort on unsorted_list.

2.

What? IndexError? Did we forget something?

We did! Some of the numbers that we’re sorting are going to be shorter than other numbers.

We can fix it though! First, we should comment out the line we added to test the sort.

3.

Where we defined digit to be the value of number_as_a_string at index index we need to now wrap that definition in a try block.

Add a try block and, indented in that block, leave your original definition of digit.

4.

After the try block, we’ll want to handle the possibility of an IndexError. What does it mean if we get an index error here?

It means the value for number at index is actually 0.

Handle the exception by adding an except IndexError block, in this case assigning digit to be 0.

5.

Excellent! Now let’s try uncommenting the line where we sort unordered_list. Print out the results.

6.

Great job! We created an algorithm that:

  • Takes numbers in an input list.
  • Passes through each digit in those numbers, from least to most significant.
  • Looks at the values of those digits.
  • Buckets the input list according to those digits.
  • Renders the results from that bucketing.
  • Repeats this process until the list is sorted.

And that’s what a radix sort does! Feel free to play around with the solution code, see if there’s anything you can improve about the code or a different way of writing it you want to try.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?