Back to All Algorithms
Bucket Sort
Category: Sorting | Time: O(n + k) | Space: O(n)
Visualization
Visual representation of the data structure
About Bucket Sort

A distribution sort that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sort algorithm. It is a distribution sort, a generalization of pigeonhole sort that allows for a larger range of keys.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Variable State
{
  "arr": "[0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434]",
  "n": 5
}
Bucket Sort: How It Works
Last updated October 28, 2026

Overview

Bucket Sort spreads values across a fixed number of buckets according to where they fall in the overall range, sorts each bucket individually, and concatenates the buckets. If values are spread evenly, each bucket holds only a few items and the whole sort runs in near-linear time.

This page's version uses 5 buckets and the index formula Math.floor(n * (x - min) / (max - min + 1)). Each bucket is sorted with the built-in sort, which is fine because buckets are expected to be small.

The default input is six decimals between 0.12 and 0.90. Because of the + 1 in the formula (which is meant for integers), every value lands in buckets 0–2 and buckets 3–4 stay empty. Watching the buckets variable in the dry run makes that uneven spread easy to see.

Step by step

  1. Find min and max of the input.
  2. Create n empty buckets.
  3. Put each value into bucket floor(n * (x - min) / (max - min + 1)).
  4. Sort each bucket (insertion sort or the built-in sort).
  5. Concatenate the buckets in order.

Complexity

CaseCostWhy
Best caseO(n + k)Values spread evenly over k buckets, so each bucket is tiny.
Average caseO(n + k)Holds for uniformly distributed input.
Worst caseO(n^2)All values fall into one bucket and that bucket is sorted with a quadratic sort.
Extra spaceO(n + k)The buckets hold every element once.

When to use it

  • Floating-point values that are roughly uniformly distributed, e.g. random numbers in [0, 1).
  • Data whose distribution you know, so you can size buckets to match it.
  • Parallel sorting: buckets can be sorted independently on different threads.

Common mistakes

  • Skewed data breaks the performance guarantee because most items pile into a few buckets.
  • The max - min + 1 denominator is right for integers but compresses fractional ranges, as the default example shows; use (max - min) with a guard for the maximum value when sorting decimals.
  • Choosing too few buckets wastes the idea; too many wastes memory.
Worked Example: Bucket Sort Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Bucket Sort: follow each row, predict the next one, and compare. The run below takes 35 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434
Result
return value = [0.1234,0.3434,0.565,0.656,0.665,0.897]
StepLineArrayWhat happens
11[0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434]n = 5
3416[0.1234, 0.3434, 0.565, 0.656, 0.665, 0.897]result = [0.1234,0.3434,0.565,0.656,0.665,0.897]
3517[0.1234, 0.3434, 0.565, 0.656, 0.665, 0.897]return value = [0.1234,0.3434,0.565,0.656,0.665,0.897]
Frequently Asked Questions
About Bucket Sort

What is Bucket Sort?

Bucket Sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets (or 'bins'). Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sort algorithm.

What is the performance of Bucket Sort?

The average-case time complexity is O(n + k), where n is the number of elements and k is the number of buckets, assuming the input is uniformly distributed. The worst-case time complexity is O(n^2) if all elements fall into a single bucket and a slow sorting algorithm is used on that bucket.

When should Bucket Sort be used?

Bucket Sort is most effective when the input data is uniformly distributed over a range. This ensures that elements are spread out evenly across the buckets, leading to optimal performance and avoiding the worst-case scenario.

Is Bucket Sort stable?

Yes, Bucket Sort can be implemented as a stable algorithm if the sorting algorithm used to sort the individual buckets is also stable (like Insertion Sort or Merge Sort).

What is the space complexity of Bucket Sort?

The space complexity is O(n + k) in the worst case, where n is the number of elements and k is the number of buckets, to store the buckets and the elements within them.

How do you determine the number of buckets?

The number of buckets (k) is a parameter that can be tuned. A common choice is to have k be approximately equal to the number of elements (n). The performance depends on having a good distribution function that maps items to buckets.

What algorithm is typically used to sort the buckets?

Insertion Sort is a common choice for sorting the individual buckets because it is very efficient for small lists, and the buckets are expected to be small if the data is uniformly distributed.

Can Bucket Sort handle negative numbers?

Yes, the distribution function just needs to be designed to handle the range of numbers, including negatives. For example, it would map the minimum value to the first bucket and the maximum value to the last.

How is Bucket Sort different from Radix Sort?

Bucket Sort distributes elements into buckets based on their value range, while Radix Sort distributes elements into buckets based on their digits. Bucket sort is often used for floating-point numbers, while Radix sort is for integers or strings.

Is Bucket Sort a comparison-based sort?

It's a mix. The distribution of elements into buckets is not comparison-based. However, the sorting of the individual buckets usually uses a comparison-based sort. Its overall efficiency comes from reducing the number of comparisons by partitioning the data first.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.