Site icon Study Algorithms

Bucket Sort

Bucket Sort is a sorting technique which puts limitations on the input set to get an improved performance. But, before we start learning about this, let us take a quick recap. This is essential so that you understand what are the use cases of Bucket Sort.

The sorting techniques that work in an average time complexity of O(n^2) are:

Some sorting techniques that have an average time complexity of O(n*\log n) are:

Counting Sort is a special sorting technique that does not work based on comparison, rather it sacrifices on space for an improved time. So after all these cases, what can we improve?

Basic Idea:

Bucket Sort algorithm works a little bit on the divide and conquer strategy. We divide the given elements into a finite number of buckets, and then sort those buckets individually. Going ahead, we use these sorted buckets to rebuild the final sorted array.

How does it work?

In the below example, we have an array with some elements that belong to a finite set. The values range from 0-25.

Fig: Sample input array

Based upon the range of values, create some buckets that hold a range of values. For example: In this example, we can create 5 buckets that hold 5 values each. These 5 buckets can have the range as:

Fig: Creating 5 buckets

Now, iterate over each of the element in the original array and store it in the respective bucket. For instance, the element 11 would go in bucket #3. Similarly, element 9 would go in bucket 2. Once all the elements have been categorized we will have some buckets of elements with us.

Fig: adding elements to buckets

We have now reached the point where we take advantage of this algorithm. The elements have been divided into smaller problems which are now easy to solve 😄. Now you can sort these buckets individually and we will have several buckets which are sorted.

Fig: Sorted buckets

Start iterating over each of these buckets and populate the final resultant array. This will give you a sorted array.

Fig: Final sorted array

Time Complexity Analysis:

For a moment, it may seem that this is the best sorting technique if we are not constrained by space. But there are a certain factors you should keep in mind:

Implementation:

  static void bucketSort(int[] arr, int noOfBuckets) {

    int globalMax = Integer.MIN_VALUE;
    int globalMin = Integer.MAX_VALUE;
    for (int i = 0; i < arr.length; i++) {
      globalMin = Math.min(arr[i], globalMin);
      globalMax = Math.max(arr[i], globalMax);
    }

    int range = globalMax - globalMin;
    int bucketRange = (int) Math.ceil((double) range / noOfBuckets);

    // Create bucket array
    List<Integer>[] buckets = new List[noOfBuckets];

    // Associate a list with each index in the bucket array
    for (int i = 0; i < noOfBuckets; i++) {
      buckets[i] = new LinkedList<>();
    }

    // Assign numbers from array to the proper bucket
    // by using hashing function
    for (int num : arr) {
      buckets[hash(num, bucketRange, noOfBuckets)].add(num);
    }

    // sort buckets
    for (List<Integer> bucket : buckets) Collections.sort(bucket);

    int idx = 0;
    // Merge buckets to get sorted array
    for (List<Integer> bucket : buckets) {
      for (int num : bucket) {
        arr[idx++] = num;
      }
    }
  }

  // A very simple hash function
  private static int hash(int num, int hashValue, int numberOfBuckets) {
    int bucketNumber = num / hashValue;
    if (bucketNumber == numberOfBuckets)
      bucketNumber--;
    return bucketNumber;
  }
Code language: Java (java)

The implementation and test cases can also be found on GitHub.

Video Explanation:

Exit mobile version