Chapters

Hide chapters

Data Structures & Algorithms in Dart

Second Edition · Flutter · Dart 3.0 · VS Code 1.78

Section VI: Challenge Solutions

Section 6: 21 chapters
Show chapters Hide chapters

18. Radix Sort
Written by Jonathan Sande

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this chapter, you’ll look at a completely different sorting model. So far, you’ve been relying on comparisons to determine the sorting order. Radix sort is a non-comparative algorithm for sorting integers. The word radix means base, as in the base of a number system. For example, decimal is base 10, and binary is base 2. You can use any base with a radix sort, but to keep things simple, this chapter will focus on sorting base-10 integers.

Radix sort relies on the position of digits within a number. For example, there are four digits in the number 1772. The least significant digit is 2 since this is in the ones place. The most significant digit is 1 since this is the thousands-place value, greater than any other place value in this particular number. The following diagram shows the place values of a four-digit number:

1 thousands 7 hundreds 7 tens 2 ones

There are multiple implementations of radix sort that focus on different problems. One type sorts by the least significant digit (LSD) and another by the most significant digit (MSD).

You’ll learn about both LSD- and MSD-radix sorting in this chapter.

Sorting by Least Significant Digit

Before you implement a radix sort in code, go through the following example to get an intuitive idea of how the sort works.

Example

An LSD-radix sort starts by looking at the least significant digits in a list of numbers. For example, the image below shows the numbers 88, 410, 1772 and 20 vertically aligned with a box drawn around the ones place:

4 6 7 0 5 7 7 0 1 8 0

Round One

You start by making ten buckets to sort the numbers into. You can think of this like sorting fruit. You put the apples in the apple bucket, the oranges in the oranges bucket, and the pears in the pear bucket.

1 4 0 9 6 0 1 3 2 8

0 50 844 1226 08 6 7 2 7 9 6 4 7 4

5 3 9 6 0 1 2 2 6 8 8

Round Two

For round two, you look at the next significant digit, the tens-place value:

5 8 9 3 3 3 5 9 9 2 5

0 5 1 6 5 2 8 6 3 6

7 58 21 5 4 7 6 2 3 1 4 4 399 8726

3 1 2 8 4 0 2 0 0 0 1

Round Three

This time look at the hundreds place. Since 20 and 88 are less than 100, you can use 0s for the hundreds place:

2 0 7 7 4 8 3 9 8 8 2 9 2

3 0 7 3 8 8 9 7 9 5 093 83 56 0694

1 3 2 4 7 8 1 1 7 6 3 4 8

Round Four

In this radix sort algorithm, there’s one round for every significant digit. Since 1722 has four digits, this fourth round will ensure that the thousands-place value is also sorted. Pad any numbers less than one thousand with zeros in front:

0 2 3 1 7 5 2 0 0 0 2 0 6 1 1 3

3 6 9 2 3 6 2 9 7 1 8295 10 50 594

8 2 4 9 2 0 0 3 3 1 6

Implementation

To implement LSD-radix sort, you’ll use a while loop for each round as you step through the place values. Your ten buckets will be ten integer lists.

Testing it Out

Head back to bin/start.dart and replace the contents of the file with the following code:

Sorting by Most Significant Digit

The MSD-radix sort uses the most significant digit to sort a list. You might think that’s a little strange when you look at a list of numbers sorted in this way:

8 9 8 1 7 9 2 9 9 9 7 3 2 2 6 9 8

z u e m m d a q e h e i n y o a o e f

Example

Start with the following unsorted list:

Beginning the Sort

As with LSD-radix sort, you first create ten buckets. However, this time you add the numbers to the buckets according to their most significant digit, that is, the first digit on their left:

7 9 6 0 7 5 3 6 0 9 78 7028 919 477 447 25

Recursively Sorting 1345 and 13

In the previous image, the ones bucket and the fours bucket both have more than one element. Start with the ones bucket, which contains 1345 and 13. Take those numbers and perform another bucket sort, this time on the second most significant digit:

7 7 0 7 8 2 0 3 7 9 04 1708

0 2 6 8 0 7 0 8 1 5 wfiehirc 97 7745

Recursively Sorting 46 and 459

Now you need to do the same thing with 46 and 459. Perform a bucket sort on the second digit. That gives the following:

5 4 1 3 4 4 5 1 2 6 286 56

Combining All the Buckets

There were no other buckets with more than one number from the original round, so you’re finished. Combine all of the buckets and sorted sublists into the final sorted result of [13, 1345, 459, 46, 500, 999]:

1 8 9 7 2 2 8 2 9 7 2 1 8 7 4 8 2

Implementation

To start off, you’ll write a few helper methods.

Getting the Number of Digits

Open lib/radix_sort.dart again and add the following int extension to the file:

Finding the Digit at Some Position

Add the following import to lib/radix_sort.dart:

Finding the Max Digits in the List

Go back to lib/radix_sort.dart and add a new List extension with the following method:

Adding the Recursive Sort Methods

Go back to the MsdRadixSort extension on List in lib/radix_sort.dart. You’re going to add two more methods to this extension. One will be public and the other a private recursive helper method.

Testing it Out

Open bin/starter.dart again and run the following code in main:

Challenges

The challenges below will help strengthen your understanding of radix sorts. You can find the answers in the Challenge Solutions section or in the supplementary materials that accompany this book.

Challenge 1: What Are in the Buckets?

Add a print statement to your radixSort implementation to tell you what’s in the buckets after each round of sorting.

Challenge 2: Unique Characters

Write a function that returns the total number of unique characters used in a list of words.

Challenge 3: Optimization

Given the following list:

Key Points

  • Unlike the other sorting algorithms you’ve implemented in previous chapters, radix sort doesn’t rely on comparing two values. It leverages bucket sort, which is a way to sort numbers by their digits.
  • The word radix means base, as in base-10 or base-2 numbering systems. The internal bucket sort will use the same number of buckets as the base.
  • Radix sort can be one of the fastest sorting algorithms for sorting values with positional notation.
  • A least-significant-digit (LSD) radix sort begins sorting with the rightmost digit.
  • Another way to implement radix sort is the most-significant-digit (MSD) form. This form sorts by prioritizing the leftmost digits over the lesser ones to the right. It’s best illustrated by the sorting behavior of the String type.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now