Skip to content
Part IA Lent Term

Bucket Sort

Overview

Bucket sort distributes nn inputs into kk buckets according to their key range, sorts each bucket individually (typically with insertion sort), and concatenates the buckets in order. It assumes the input is drawn uniformly from a known interval. Under this assumption, each bucket receives approximately n/kn/k elements and the average-case running time is Θ(n)\Theta(n).

Worst-case running time is Θ(n2)\Theta(n^2) if all elements fall into one bucket, since insertion sort on nn elements is Θ(n2)\Theta(n^2).

Algorithm

Bucket sort diagram

For input in the range [0,1)[0, 1):

  1. Create nn empty buckets (linked lists or arrays).
  2. For each element xx, insert it into bucket nx\lfloor n \cdot x \rfloor.
  3. Sort each bucket (e.g. with insertion sort).
  4. Concatenate buckets in order (0, 1, …, n1n-1).

The bucket index nx\lfloor n \cdot x \rfloor maps the uniform [0,1)[0, 1) range onto nn equal-width intervals.

Worked Example

Input: [0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68][0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68], n=10n = 10.

Step 2: Distribute into buckets (10x\lfloor 10 \cdot x \rfloor):

BucketRangeElements
0[0,0.1)[0, 0.1)
1[0.1,0.2)[0.1, 0.2)0.17, 0.12
2[0.2,0.3)[0.2, 0.3)0.26, 0.21, 0.23
3[0.3,0.4)[0.3, 0.4)0.39
4[0.4,0.5)[0.4, 0.5)
5[0.5,0.6)[0.5, 0.6)
6[0.6,0.7)[0.6, 0.7)0.68
7[0.7,0.8)[0.7, 0.8)0.78, 0.72
8[0.8,0.9)[0.8, 0.9)
9[0.9,1.0)[0.9, 1.0)0.94

Step 3: Sort each bucket (insertion sort):

  • Bucket 1: [0.12,0.17][0.12, 0.17]
  • Bucket 2: [0.21,0.23,0.26][0.21, 0.23, 0.26]
  • Bucket 3: [0.39][0.39]
  • Bucket 6: [0.68][0.68]
  • Bucket 7: [0.72,0.78][0.72, 0.78]
  • Bucket 9: [0.94][0.94]

Step 4: Concatenate: [0.12,0.17,0.21,0.23,0.26,0.39,0.68,0.72,0.78,0.94][0.12, 0.17, 0.21, 0.23, 0.26, 0.39, 0.68, 0.72, 0.78, 0.94]

Complexity Analysis

Distribution into nn buckets: Θ(n)\Theta(n). Sorting each bucket: let nin_i be the number of elements in bucket ii. Insertion sort on bucket ii costs O(ni2)O(n_i^2). Total:

T(n)=Θ(n)+i=0n1O(ni2)T(n) = \Theta(n) + \sum_{i=0}^{n-1} O(n_i^2)

Under the uniform distribution assumption, the expected value of ni2n_i^2 is 21/n2 - 1/n, giving T(n)=Θ(n)+nO(21/n)=Θ(n)T(n) = \Theta(n) + n \cdot O(2 - 1/n) = \Theta(n).

If the uniformity assumption fails and all nn elements land in one bucket, insertion sort on that bucket costs Θ(n2)\Theta(n^2), which is the worst case.

Comparison with Counting Sort and Radix Sort

SortAssumptionAverageWorst
Counting sortInteger keys in [0,k][0, k]Θ(n+k)\Theta(n + k)Θ(n+k)\Theta(n + k)
Radix sortdd-digit integersΘ(d(n+k))\Theta(d(n + k))Θ(d(n+k))\Theta(d(n + k))
Bucket sortUniform real keys in [0,1)[0, 1)Θ(n)\Theta(n)Θ(n2)\Theta(n^2)

Bucket sort is the only one of the three whose worst case is quadratic. Its average-case speed depends on the uniform-distribution assumption, whereas counting and radix sort guarantee their bounds for all inputs satisfying their range constraints.

Practical Use

Bucket sort is most useful when input is known to be uniformly distributed — for example, hashing a uniform key space into buckets. In practice, it is often combined with other techniques (e.g. using a better sort within buckets, or using it as one phase of a hybrid algorithm).

Summary

PropertyValue
TypeNon-comparison, distribution sort
Average timeΘ(n)\Theta(n)
Worst-case timeΘ(n2)\Theta(n^2)
SpaceΘ(n)\Theta(n) for buckets
StableDepends on bucket sort
AssumptionUniform distribution of keys
Beats Ω(nlogn)\Omega(n \log n)Yes on average (not comparison-based)