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.
import { insertionSort } from '../insertion-sort/insertion-sort';function createBuckets(list: number[], bucketSize: number) { let min = list[0]; let max = list[0]; for (let i = 1; i < list.length; i++) { if (list[i] < min) { min = list[i]; } if (list[i] > max) { max = list[i]; } } const bucketCount = Math.floor((max - min) / bucketSize) + 1; const buckets: number[][] = new Array(bucketCount).fill(null).map((_) => []); for (let i = 0; i < list.length; i++) { const bucket = Math.floor((list[i] - min) / bucketSize); buckets[bucket].push(list[i]); } return buckets;}/** * @param list * @param bucketSize The algorithm executes its best scenario when the elements can be * distributed into the buckets evenly. i.e. if the elements are densely allocated, then * using fewer buckets is better. By default this function will use a bucket size equal to 3. */export function bucketSort(list: number[], bucketSize = 3) { if (list.length < 2) { return list; } const sortedList: number[] = []; const buckets = createBuckets(list, bucketSize); for (let i = 0; i < buckets.length; i++) { const bucket = buckets[i]; sortedList.push(...insertionSort(bucket)); } return sortedList;}
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.
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
No, Bucket Sort is not in-place. It requires O(n + k) additional space for buckets.
Adaptive
Partially adaptive depending on the distribution of data and the sorting algorithm used for individual buckets.
Online
No, Bucket Sort is not online. It needs to know the range (min/max) before distributing elements.
Let n = number of elementsLet k = number of bucketsDistribution: O(n)Sorting k buckets with ~n/k elements each: k × O((n/k)²) = O(n²/k)If k = Θ(n), then O(n²/n) = O(n)Total: O(n + n) = O(n)
function createBuckets(list: number[], bucketSize: number) { // Find the range of values let min = list[0]; let max = list[0]; for (let i = 1; i < list.length; i++) { if (list[i] < min) min = list[i]; if (list[i] > max) max = list[i]; } // Calculate number of buckets needed const bucketCount = Math.floor((max - min) / bucketSize) + 1; // Initialize empty buckets const buckets: number[][] = new Array(bucketCount) .fill(null) .map((_) => []); // Distribute elements into buckets for (let i = 0; i < list.length; i++) { const bucket = Math.floor((list[i] - min) / bucketSize); buckets[bucket].push(list[i]); } return buckets;}