
Imagine that you’re a teacher grading your students’ essays. You’re dividing the essays into piles based on their letter grade: A, B, C, D, or F.
In order to sort the essays by grade, you don’t need to compare one essay to another. Maybe the essays were evenly distributed across each letter grade, or maybe every student got a B — it doesn’t matter! You just need to know each essay’s grade in order to put it in the correct pile. That’s how distribution sort works.

After you divide your students’ essays by letter grade, you have 10 essays in the B pile with these grades:
[87, 84, 89, 84, 82, 85, 80, 88, 81, 84]
Now, you want to sort all of those essays by their numerical grade. What type of sort would you use?
In this lesson, we’ll cover two common implementations of the distribution sort method: bucket sort and radix sort.
For both bucket and radix sorts, the most important thing you need to decide is: How many buckets should there be?
The answer? It depends on your data set. If you were sorting a lot of elements alphabetically, 26 buckets (one per letter) might work. Or, if you were sorting numbers with radix sort, 10 buckets (one for each digit) could be a good starting place.
A general rule of thumb: If there’s no one obvious way to create your buckets, use the square root of the number of items you’re sorting. So, if you have an array of 100 elements, 10 buckets would be appropriate.
Bucket and radix sorts are most useful when you have a relatively dense range of numbers. “Dense” just means that the values in your starting array are relatively close together.
However, you don’t want your data set to be too dense. If your data set is very dense (i.e., its values are all the same or very similar), it would all be sorted into one bucket. This makes bucket sort’s efficiency a slow O(N^2)!

Here’s the general idea of how bucket sort works:
[29, 25, 3, 49, 9, 37, 21, 43].0-16; Bucket 2, 17-32; Bucket 3, 33-49.[3, 9]; Bucket 2, [29, 25, 21]; Bucket 3, [49, 37, 43].[3, 9]; Bucket 2, [21, 25, 29]; Bucket 3, [37, 43, 49].
Can you guess bucket sort’s Big O space efficiency?
Without getting too deep into the weeds, radix sort operates by looking at the individual digits in a set of numbers. Radix starts at either the beginning of a number (the most significant digit approach) or the end of a number (the least significant digit approach) and works through each digit until it’s reached the end and the values are sorted.
Let’s use the least significant digit approach on this unsorted array: [270, 65, 30, 603, 3, 84]. (If you’re thinking that this array isn’t dense enough for radix sort, you’re correct! We’re just using it here for the purpose of illustration.)
[270, 30, 603, 3, 84, 65].[603, 3, 30, 65, 270, 84]. (Note that if a number doesn’t have anything in the tens’ place, like 3, we act like there’s a 0 there.)[3, 30, 65, 84, 270, 603].Like magic, right?
You removed the top earners’ salaries from the database, so you’re left with this data set. It’s denser now and perfect for bucket sort!
| Salary |
|---|
| 66,000 |
| 71,000 |
| 59,000 |
| 52,000 |
| 68,000 |
| 55,000 |
How many buckets would you use to sort this data?
You’re more likely to be asked about bubble, insertion, merge, or quick sorts in an interview than bucket or radix sorts. But it’s important to know how distribution sorts work, how they differ from comparison sorts, and the basics of bucket and radix sorts.
To get a better sense of how these algorithms work, use this visualization tool!
Because we won’t discuss how to implement radix sort in code, you can read more about it here.
