Bucket Sort Algorithm: How It Works & When to Use It

Muhaymin Bin Mehmood

Muhaymin Bin Mehmood

· 5 min read
Bucket Sort Algorithm: How It Works & When to Use It Banner Image
Bucket Sort Algorithm: How It Works & When to Use It Banner Image

Introduction

Sorting is a fundamental operation in computer science, and choosing the right algorithm can significantly impact performance. Bucket Sort is an efficient sorting technique that excels when dealing with a uniform distribution of numbers. In this blog, we will explore what Bucket Sort is, how it works, its time complexity, advantages, disadvantages, and implementation in JavaScript and Python.

Table of Contents

  1. What is Bucket Sort?
  2. How Does Bucket Sort Work?
  3. Step-by-Step Implementation of Bucket Sort
  4. Bucket Sort Algorithm with Code Examples
    • 4.1 Bucket Sort in Python
    • 4.2 Bucket Sort in JavaScript
  5. Time Complexity of Bucket Sort
  6. Space Complexity of Bucket Sort
  7. Advantages and Disadvantages of Bucket Sort
  8. When to Use Bucket Sort?
  9. Bucket Sort vs Other Sorting Algorithms
  10. Real-World Applications of Bucket Sort
  11. Conclusion

What is Bucket Sort?

Bucket Sort is a comparison-based sorting algorithm that distributes elements into multiple “buckets,” sorts each bucket individually, and then merges them to produce the final sorted array.

It works best when input data is uniformly distributed over a known range, making it particularly efficient for floating-point numbers, percentages, or large datasets with minimal variation.

How Bucket Sort Works

The Bucket Sort algorithm follows these steps:

  • Create Buckets: Divide the input elements into a fixed number of buckets.
  • Distribute Elements: Place each element into its respective bucket based on a calculated index.
  • Sort Individual Buckets: Sort each bucket using an appropriate algorithm (Insertion Sort, Merge Sort, or Quick Sort).
  • Concatenate Buckets: Combine the sorted buckets to get the final sorted array.

Example Walkthrough

Let’s say we need to sort the array [0.42, 0.32, 0.77, 0.25, 0.61, 0.89] using Bucket Sort:

  • Create Buckets: Assume 5 buckets for values ranging from 0 to 1.
  • Distribute Elements:
    • Bucket 1: [0.25]
    • Bucket 2: [0.32, 0.42]
    • Bucket 3: [0.61]
    • Bucket 4: [0.77]
    • Bucket 5: [0.89]
  • Sort Buckets:
    • Bucket 2: [0.32, 0.42] (sorted using Insertion Sort)
  • Concatenate Buckets:[0.25, 0.32, 0.42, 0.61, 0.77, 0.89]

Time Complexity of Bucket Sort

Case Time Complexity
Best CaseO(n + k)
Average CaseO(n + k)
Worst CaseO(n^2)
  • n = number of elements
  • k = number of buckets

The best and average cases occur when elements are distributed evenly across buckets, and sorting each bucket is efficient. The worst case occurs when all elements fall into a single bucket, making Bucket Sort equivalent to O(n^2) sorting algorithms like Bubble Sort.

Advantages of Bucket Sort

  • Fast for Uniformly Distributed Data: Works efficiently when elements are evenly spread.
  • Efficient for Floating-Point Numbers: Handles fractional values well.
  • Can Leverage Other Sorting Algorithms: Buckets can use efficient sorts like Quick Sort or Merge Sort.
  • Parallelizable: Sorting individual buckets can be done concurrently.

Disadvantages of Bucket Sort

  • Requires Extra Space: Needs additional storage for buckets.
  • Not Suitable for Large Range Inputs: Performance suffers when data is not uniformly distributed.
  • Complex Implementation: More intricate than simpler sorts like Insertion Sort.

Bucket Sort Implementation in JavaScript

function bucketSort(arr, bucketSize = 5) {
    if (arr.length === 0) return arr;

    let min = Math.min(...arr);
    let max = Math.max(...arr);
    let bucketCount = Math.floor((max - min) / bucketSize) + 1;
    let buckets = Array.from({ length: bucketCount }, () => []);

    arr.forEach(num => {
        let index = Math.floor((num - min) / bucketSize);
        buckets[index].push(num);
    });

    return buckets.reduce((sortedArr, bucket) => {
        return sortedArr.concat(bucket.sort((a, b) => a - b));
    }, []);
}

console.log(bucketSort([42, 32, 77, 25, 61, 89]));

Bucket Sort Implementation in Python

def bucket_sort(arr, bucket_size=5):
    if len(arr) == 0:
        return arr
    
    min_val, max_val = min(arr), max(arr)
    bucket_count = (max_val - min_val) // bucket_size + 1
    buckets = [[] for _ in range(bucket_count)]

    for num in arr:
        index = (num - min_val) // bucket_size
        buckets[index].append(num)
    
    sorted_arr = []
    for bucket in buckets:
        sorted_arr.extend(sorted(bucket))
    
    return sorted_arr

print(bucket_sort([42, 32, 77, 25, 61, 89]))

When to Use Bucket Sort?

Best Use Cases:

  • When input data is uniformly distributed.
  • When sorting floating-point numbers or decimal values.
  • When dealing with large datasets and can leverage parallel processing.

Avoid When:

  • Data is highly skewed (non-uniform distribution).
  • Memory is limited (extra space required for buckets).
  • Elements range over a wide interval, making bucket allocation inefficient.

Conclusion: Should You Use Bucket Sort?

Bucket Sort is a powerful algorithm when applied correctly. If your dataset has a uniform distribution and can fit into a predefined range, it can outperform many traditional sorting methods. However, for general-purpose sorting, algorithms like Quick Sort or Merge Sort might be better choices.

If you are working with floating-point numbers, percentages, or large datasets with uniform distribution, Bucket Sort is an excellent choice for optimal performance.

FAQs on Bucket Sort

Q1. Is Bucket Sort stable?

Yes, Bucket Sort can be stable if elements in each bucket maintain their original order when sorted.

Q2. What is the best case time complexity of Bucket Sort?

The best case occurs when elements are evenly distributed, leading to O(n + k) time complexity.

Q3. Can Bucket Sort be used for negative numbers?

Yes, but additional logic is needed to handle negative values when distributing elements into buckets.

Q4. Is Bucket Sort better than Quick Sort?

It depends on the dataset. Quick Sort is generally faster for general sorting, while Bucket Sort excels with uniformly distributed data.

Related Blogs

Muhaymin Bin Mehmood

About Muhaymin Bin Mehmood

Front-end Developer skilled in the MERN stack, experienced in web and mobile development. Proficient in React.js, Node.js, and Express.js, with a focus on client interactions, sales support, and high-performance applications.

Join our newsletter

Subscribe now to our newsletter for regular updates.

Copyright © 2025 Mbloging. All rights reserved.