Program to implement Bucket Sort in JavaScript

Bucket Sort is an efficient sorting algorithm that works by distributing elements into multiple buckets based on their values, then sorting each bucket individually. This approach is particularly effective when the input is uniformly distributed across a range.

How Bucket Sort Works

The algorithm follows these key steps:

  1. Find the minimum and maximum values in the array
  2. Create a specific number of buckets to hold ranges of values
  3. Distribute array elements into appropriate buckets
  4. Sort each bucket using a suitable sorting algorithm (like insertion sort)
  5. Concatenate all sorted buckets to get the final result

Algorithm Steps

1. Create the bucketSort function
2. Handle edge cases (empty array)
3. Find min and max values
4. Calculate bucket count and create buckets
5. Distribute elements into buckets
6. Sort individual buckets
7. Merge sorted buckets back into original array

Implementation

const arr = [32, 6, 34, 4, 78, 1, 6767, 4, 65, 34, 879, 7];

const bucketSort = arr => {
    if (arr.length === 0) {
        return arr;
    }
    
    let i,
        minValue = arr[0],
        maxValue = arr[0],
        bucketSize = 5;
    
    // Find min and max values
    arr.forEach(function (currentVal) {
        if (currentVal  maxValue) {
            maxValue = currentVal;
        }
    });
    
    // Calculate number of buckets needed
    let bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
    let allBuckets = new Array(bucketCount);
    
    // Initialize empty buckets
    for (i = 0; i  {
    let length = arr.length;
    let i, j;
    
    for(i = 1; i = 0 && arr[j] > temp; j--) {
            arr[j+1] = arr[j];
        }
        arr[j+1] = temp;
    }
    return arr;
};

console.log("Original array:", arr);
console.log("Sorted array:", bucketSort([...arr])); // Using spread to avoid modifying original
Original array: [ 32, 6, 34, 4, 78, 1, 6767, 4, 65, 34, 879, 7 ]
Sorted array: [ 1, 4, 4, 6, 7, 32, 34, 34, 65, 78, 879, 6767 ]

Time and Space Complexity

Case Time Complexity Space Complexity
Best Case O(n + k) O(n + k)
Average Case O(n + k) O(n + k)
Worst Case O(n²) O(n + k)

Where n is the number of elements and k is the number of buckets.

Key Points

  • Bucket Size: Choosing the right bucket size affects performance. Too few buckets may cause uneven distribution
  • Input Distribution: Works best with uniformly distributed data
  • Stability: Can be made stable depending on the sorting algorithm used for individual buckets
  • Use Cases: Ideal for sorting floating-point numbers or when input range is known

Conclusion

Bucket Sort offers excellent performance for uniformly distributed data with O(n + k) average time complexity. It's particularly useful when the input range is known beforehand and can be evenly distributed across buckets.

Updated on: 2026-03-15T23:19:00+05:30

732 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements