LibraryBucket Sort

Bucket Sort

Learn about Bucket Sort as part of GATE Computer Science - Algorithms and Data Structures

Bucket Sort: An Efficient Sorting Algorithm

Bucket Sort is a comparison-based 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 bucket sort. Finally, the elements from the buckets are concatenated in order to get the sorted array.

How Bucket Sort Works

The core idea behind Bucket Sort is to divide the input range into a fixed number of buckets. The efficiency of Bucket Sort heavily relies on the assumption that the input data is uniformly distributed over a range. If the data is not uniformly distributed, the performance can degrade significantly.

Bucket Sort distributes elements into buckets for efficient sorting.

Elements are placed into buckets based on their values. For example, if we have numbers from 0 to 99 and 10 buckets, bucket 0 would hold numbers 0-9, bucket 1 would hold 10-19, and so on. Each bucket is then sorted independently.

The algorithm typically involves these steps:

  1. Create n empty buckets, where n is usually chosen based on the input size or range.
  2. Iterate through the input array. For each element, determine which bucket it belongs to based on its value and the range covered by each bucket. Place the element into the corresponding bucket.
  3. Sort each non-empty bucket individually. Common choices for sorting within buckets include Insertion Sort or recursively applying Bucket Sort if buckets become too large.
  4. Concatenate the sorted elements from all buckets in order to produce the final sorted array.

Time Complexity Analysis

The time complexity of Bucket Sort is highly dependent on the distribution of the input data and the sorting algorithm used within the buckets. When the input is uniformly distributed, Bucket Sort can achieve an average time complexity of O(n + k), where 'n' is the number of elements and 'k' is the number of buckets. If Insertion Sort is used within buckets, and the number of elements per bucket is small and constant on average, the overall complexity remains O(n + k).

Bucket Sort excels when the input data is uniformly distributed across a range. Its efficiency hinges on this assumption.

However, in the worst-case scenario, where all elements fall into a single bucket, and the sorting algorithm within the bucket has a complexity of O(m^2) (like Insertion Sort for 'm' elements), the overall complexity can degrade to O(n^2). If a comparison sort like Merge Sort (O(m log m)) is used within buckets, the worst-case complexity becomes O(n log n).

Space Complexity

The space complexity of Bucket Sort is O(n + k), where 'n' is the number of elements and 'k' is the number of buckets. This is because we need to store the elements in the buckets, and potentially auxiliary space for the sorting algorithm used within the buckets.

When to Use Bucket Sort

Bucket Sort is particularly useful for sorting floating-point numbers or integers that are known to be within a specific range and are expected to be distributed relatively evenly. It's often used as a subroutine in other algorithms or for specific types of data where its assumptions hold true.

What is the primary condition for Bucket Sort to achieve its optimal time complexity?

Uniform distribution of input data across the range.

Imagine sorting a list of student scores (0-100) for a class of 50. We can create 10 buckets, each representing a range of 10 points (0-9, 10-19, ..., 90-100). We then place each student's score into the corresponding bucket. After all scores are distributed, we sort the scores within each bucket (e.g., using insertion sort for small groups). Finally, we combine the sorted buckets to get the overall sorted list of scores. This visualization shows the distribution and subsequent sorting within buckets.

📚

Text-based content

Library pages focus on text content

Comparison with Other Sorting Algorithms

AlgorithmAverage Time ComplexityWorst-Case Time ComplexitySpace ComplexityStability
Bucket SortO(n + k)O(n^2) or O(n log n)O(n + k)Depends on inner sort
Merge SortO(n log n)O(n log n)O(n)Stable
Quick SortO(n log n)O(n^2)O(log n) (average)Not Stable
Insertion SortO(n^2)O(n^2)O(1)Stable

Example Scenario

Consider sorting an array of 10 floating-point numbers between 0.0 and 1.0: [0.897, 0.565, 0.656, 0.123, 0.665, 0.343, 0.001, 0.987, 0.234, 0.765]. We can use 10 buckets, where bucket

code
i
stores numbers in the range
code
[i/10, (i+1)/10)
.

  • Bucket 0: [0.001, 0.123, 0.234]
  • Bucket 1: []
  • Bucket 2: []
  • Bucket 3: [0.343]
  • Bucket 4: []
  • Bucket 5: [0.565]
  • Bucket 6: [0.656, 0.665]
  • Bucket 7: [0.765]
  • Bucket 8: []
  • Bucket 9: [0.897, 0.987]

After sorting each bucket (e.g., using insertion sort), we concatenate them: [0.001, 0.123, 0.234, 0.343, 0.565, 0.656, 0.665, 0.765, 0.897, 0.987].

If all elements in an array fall into a single bucket, what is the worst-case time complexity of Bucket Sort if Insertion Sort is used within buckets?

O(n^2)

Learning Resources

Bucket Sort - GeeksforGeeks(documentation)

A comprehensive explanation of Bucket Sort, including its working, time complexity, and implementation examples in C++ and Java.

Bucket Sort Algorithm - Tutorialspoint(tutorial)

Provides a clear overview of the Bucket Sort algorithm, its steps, and a detailed analysis of its time and space complexity.

Bucket Sort Explained - YouTube(video)

A visual explanation of the Bucket Sort algorithm, demonstrating how elements are distributed and sorted into buckets.

Bucket Sort - Programiz(documentation)

Offers a concise explanation of Bucket Sort with a focus on its application and a Python implementation.

Sorting Algorithms: Bucket Sort - Coursera Blog(blog)

Discusses the principles of Bucket Sort, its advantages, disadvantages, and when it is most effective.

Bucket Sort - Wikipedia(wikipedia)

A detailed overview of Bucket Sort, its variations, historical context, and theoretical underpinnings.

Introduction to Algorithms (CLRS) - Chapter 8: Sorting in Linear Time(paper)

While not a direct link to the chapter online, this is the seminal textbook that covers Bucket Sort in detail within its chapter on linear-time sorting algorithms. It's a crucial resource for competitive exam preparation.

Bucket Sort Implementation in Python - Real Python(blog)

This article covers various sorting algorithms in Python, including a section or example that can be adapted for Bucket Sort, focusing on practical implementation.

Data Structures and Algorithms - Bucket Sort - InterviewBit(documentation)

Provides a concise explanation and examples relevant to competitive programming and interviews, focusing on the practical aspects of Bucket Sort.

Bucket Sort - Visualgo(documentation)

An interactive visualization tool that allows users to see how Bucket Sort works step-by-step, aiding in conceptual understanding.