ArticleslgStudy

computer science

Bucket sort

Bucket sort is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bucket sort rather than just read about it. In short: Bucket sort, or bin sort, is a sorting algorithm 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 sorting algorithm.

Bucket sort — main illustration
Bucket sort — illustration

Key takeaways

  • Bucket sort belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bucket sort to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bucket sort from memory before moving on to harder problems.

Reference excerpt

Bucket sort, or bin sort, is a sorting algorithm 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 sorting algorithm. It is a distribution sort, a generalization of pigeonhole sort that allows multiple keys per bucket, and is a cousin of radix sort in the most-to-least significant digit flavor. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. The computational complexity depends on the algorithm used to sort each bucket, the number of buckets to use, and whether the input is uniformly distributed. Bucket sort works as follows:

Set up an array of initially empty "buckets". Scatter: Go over the original array, putting each object in its bucket. Sort each non-empty bucket. Gather: Visit the buckets in order and put all elements back into the original array.

Pseudocode function bucketSort(array, k) is buckets ← new array of k empty lists M ← 1 + the maximum key value in the array for i = 0 to length(array) do insert array[i] into buckets[floor(k × array[i] / M)] for i = 0 to k do nextSort(buckets[i]) return the concatenation of buckets[0], ...., buckets[k]

Let array denote the array to be sorted and k denote the number of buckets to use. One can compute the maximum key value in linear time by iterating over all the keys once. The floor function must be used to convert a floating number to an integer ( and possibly casting of datatypes too ). The function nextSort is a sorting function used to sort each bucket. Conventionally, insertion sort is used due to its relatively high performance with small numbers of elements, but other algorithms could be used as well, such as selection sort or merge sort. Using bucketSort itself as nextSort produces a relative of radix sort; in particular, the case n = 2 corresponds to quicksort (although potentially with poor pivot choices).

Analysis

Worst-case analysis When the input contains several keys that are close to each other (clustering), those elements are likely to be placed in the same bucket, which results in some buckets containing more elements than average. The worst-case scenario occurs when all the elements are placed in a single bucket. The overall performance would then be dominated by the algorithm used to sort each bucket, for example O(n2) insertion sort or O(nlog⁡(n)) comparison sort algorithms, such as merge sort.

Average-case analysis Consider the case that the input is uniformly distributed. The first step, which is initialize the buckets and find the maximum key value in the array, can be done in O(n) time. If division and multiplication can be done in constant time, then scattering each element to its bucket also costs O(n). Assume insertion sort is used to sort each bucket, then the third step costs O(∑i=1kni2), where ni is the length of the bucket indexed i. Since we are concerning the average time, the expectation E(ni2) has to be evaluated instead. Let Xij be the random variable that is 1 if element j is placed in bucket i, and 0 otherwise. We have ni=∑j=1nXij. Therefore,

E(ni2)=E(∑j=1nXij∑l=1nXil)=E(∑j=1n∑l=1nXijXil)=E(∑j=1nXij2)+E(∑1≤j,l≤n∑j≠lXijXil). The last line separates the summation into the case j=l and the case j≠l. Since the chance of an object distributed to bucket i is 1/k, Xij is 1 with probability 1/k and 0 otherwise.

E(Xij2)=12⋅(1k)+02⋅(1−1k)=1k E(XijXik)=1⋅(1k)(1k)=1k2 With the summation, it would be

E(∑j=1nXij2)+E(∑1≤j,k≤n∑j≠kXijXik)=n⋅1k+n(n−1)⋅1k2=n2+nk−nk2 Finally, the complexity would be O(∑i=1kE(ni2))=O(∑i=1kn2+nk−nk2)=O(n2k+n). The last step of bucket sort, which is concatenating all the sorted objects in each bucket, requires O(k) time. Therefore, the total complexity is O(n+n2k+k). Note that if k is chosen to be k=Θ(n), then bucket sort runs in O(n) average time, given a uniformly distributed input.

Optimizations A common optimization is to put the unsorted elements of the buckets back in the original array first, then run insertion sort over the complete array; because insertion sort's runtime is based on how far each element is from its final position, the number of comparisons remains relatively small, and the memory hierarchy is better exploited by storing the list contiguously in memory. If the input distribution is known or can be estimated, buckets can often be chosen which contain constant density (rather than merely having constant size). This allows O(n) average time complexity even without uniformly distributed input.

Variants

Generic bucket sort The most common variant of bucket sort operates on a list of n numeric inputs between zero and some maximum value M and divides the value range into b buckets each of size M/b. If each bucket is sorted using insertion sort, the sort can be shown to run in expected linear time (where the average is taken over all possible inputs). However, the performance of this sort degrades with clustering; if many values occur close together, they will all fall into a single bucket and be sorted slowly. This performance degradation is avoided in the original bucket sort algorithm by assuming that the input is generated by a random process that distributes elements uniformly over the interval [0,1).

ProxmapSort

Similar to generic bucket sort as described above, ProxmapSort works by dividing an array of keys into subarrays via the use of a "map key" function that preserves a partial ordering on the keys; as each key is added to its subarray, insertion sort is used to keep that subarray sorted, resulting in the entire array being in sorted order when ProxmapSort completes. ProxmapSort differs from bucket sorts in its use of the map key to place the data approximately where it belongs in sorted order, producing a "proxmap" — a proximity mapping — of the keys.

Histogram sort Another variant of bucket sort known as histogram sort or counting sort adds an initial pass that counts the number of elements that will fall into each bucket using a count array. Using this information, the array values can be arranged into a sequence of buckets in-place by a sequence of exchanges, leaving no space overhead for bucket storage.

… excerpt ends here. Continue reading the full article.

Illustrations

Bucket sort: Elements are distributed among bins
Elements are distributed among bins
Bucket sort: Then, elements are sorted within each bin
Then, elements are sorted within each bin

Worked examples

Example 1 — a first encounter with Bucket sort

Start with the simplest possible case. Write down what Bucket sort claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bucket sort before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bucket sort ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bucket sort

In research
Bucket sort appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bucket sort in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bucket sort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Sorting algorithms, Stable sorts, so understanding it makes those chapters shorter.
In everyday life
Look for Bucket sort outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bucket sort” →

How to study Bucket sort in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bucket sort means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bucket sort out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bucket sort in simple terms?

Bucket sort, or bin sort, is a sorting algorithm 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 sorting algorithm.

Why does Bucket sort matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bucket sort?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bucket sort.

Tags

  • Sorting algorithms
  • Stable sorts

Keep exploring