Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dfernandeza/data-structures-and-algorithms/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Bucket Sort is a distribution-based sorting algorithm that works by distributing elements into several buckets, sorting each bucket individually (typically using another sorting algorithm), and then concatenating the sorted buckets. It achieves linear time complexity when input is uniformly distributed across the range.Implementation
How It Works
- Find Range: Determine the minimum and maximum values in the array
- Create Buckets: Divide the range into a number of equally-sized buckets
- Distribute: Place each element into its appropriate bucket based on its value
- Sort Buckets: Sort each bucket individually (using Insertion Sort in this implementation)
- Concatenate: Combine all sorted buckets to produce the final sorted array
Bucket Sort performs best when elements are uniformly distributed. If all elements fall into the same bucket, performance degrades to the sorting algorithm used for individual buckets.
Complexity Analysis
Time Complexity
- Best Case: O(n + k) - uniform distribution
- Average Case: O(n + k) where k is number of buckets
- Worst Case: O(n²) - all elements in one bucket
Space Complexity
- Space: O(n + k) for buckets and output
- Auxiliary: O(n) for bucket storage
Usage Example
Visual Example
Let’s trace through sorting[29, 25, 3, 49, 9, 37, 21, 43] with bucketSize=10:
Characteristics
Stability
Stability
Bucket Sort can be stable if the algorithm used to sort individual buckets is stable. Since this implementation uses Insertion Sort (which is stable), the overall algorithm is stable.
In-Place
In-Place
No, Bucket Sort is not in-place. It requires O(n + k) additional space for buckets.
Adaptive
Adaptive
Partially adaptive depending on the distribution of data and the sorting algorithm used for individual buckets.
Online
Online
No, Bucket Sort is not online. It needs to know the range (min/max) before distributing elements.
When to Use
Bucket Sort excels when input is uniformly distributed over a known range.
- Uniformly distributed floating-point numbers in a known range (e.g., [0, 1))
- External sorting when data is too large for memory
- Data with a known distribution pattern
- When linear time sorting is needed and data fits the criteria
- Parallel processing (buckets can be sorted independently)
- Data is not uniformly distributed (leads to unbalanced buckets)
- Range is unknown or very large
- Memory is limited (requires extra space)
- Data is already sorted or nearly sorted (simpler algorithms may be better)
Bucket Size Selection
Performance Insights
Best Case: O(n + k)
Occurs when:- Elements are uniformly distributed
- Each bucket gets approximately n/k elements
- Sorting each bucket is O(1) on average
- Total: O(n) distribution + O(k) bucket sorting = O(n + k)
Worst Case: O(n²)
Occurs when:- All elements fall into a single bucket
- Degenerates to the sorting algorithm used for buckets
- With Insertion Sort: O(n²)
Average Case Analysis
Code Walkthrough
Helper Function: createBuckets()
Main Function: bucketSort()
Comparison with Other Sorts
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable | Distribution |
|---|---|---|---|---|---|---|
| Bucket Sort | O(n + k) | O(n + k) | O(n²) | O(n + k) | Yes* | Required |
| Counting Sort | O(n + k) | O(n + k) | O(n + k) | O(n + k) | Yes | Not required |
| Radix Sort | O(d(n + k)) | O(d(n + k)) | O(d(n + k)) | O(n + k) | Yes | Not required |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | Not required |
Real-World Applications
Where Bucket Sort Shines:
- Floating-Point Numbers: Sorting numbers in [0.0, 1.0) range
- External Sorting: When data is too large for RAM
- Parallel Processing: Buckets can be distributed across multiple processors
- Geographic Data: Sorting by latitude/longitude in bounded regions
- Histogram Generation: Creating frequency distributions
- Load Balancing: Distributing tasks across servers