Algorithms Tutorial

Searching Algorithms

Dynamic Programming

Graph Algorithms

Backtracking Algorithms


Operating System Algorithms

Miscellaneous Topics

Bucket Sort Algorithm: What It Is, Time Complexity, Example, and Drawbacks

Bucket Sort Algorithm: In this tutorial, we will learn about the bucket sort algorithm, the steps to implement, time complexity, example, and drawbacks. By Abhishek Kataria Last updated : August 12, 2023

Bucket Sort

Bucket sort is a sorting technique in which array is partitioned into the buckets. By this, each bucket will be sorted individually, by using some another sorting algorithm such as insertion sort. This sorting technique assumes that the input is drawn from a uniform distribution by which it has an average case of O(n). Bucket sort is a fast technique because bucket sort presumes that the input is generated by a process, which will distribute the element uniformly and independently over the interval.

Bucket Sort Algorithm

The following are the steps of implementing the Bucket sort:

  1. Set up an array of initially empty buckets.
  2. Put the element in the corresponding bucket.
  3. Sort each non-empty bucket.
  4. Visit the bucket in order and put all the elements into a sequence and print them.

Bucket Sort Pseudo Code

Consider the below-given pseudo code for implementing a bucket sort:

void bucketsort (int a[ ], int n, int max){
    int i,j=0;
    //initialize each bucket 0 and then make bucket empty.
    int* buckets = calloc(max+1, size of (int));
    for(int i=0; i<n; i++)
    //now sort each bucket individually.
    //sequentially empty each bucket in some array.
    for(i=0; i<max; i++)
	    while (buckets[i]--)
    //display the array b as sorted list of elements.

Bucket Sort Example

Let us sort the elements by using bucket sort. Elements which need to be sort are 56, 12, 84, 28, 0,-13, 47, 94, 31, 12.

Step 1) First set up an array which is given below:

Bucket sort 1

Step 2) Now consider the range such that:

-20 to -1, 0 to 10 10 to 20, 20 to 30, 30 to 40, 40 to 50, 50 to 60, 60 to 70, 70 to 80, 80 to 90, 90 to 100.

Now we fill up each bucket by given elements,

Bucket sort 2

Step 3) Now sort the each bucket and then print the array by visiting each bucket sequentially.

Bucket sort 3

-13, 0, 12, 12, 28, 31, 47, 56, 84, 94

This is the sorted list.

Bucket Sort Drawbacks

  1. For the bucket sort, it's the necessary part that the maximum value of the element must be known.
  2. In this type of technique, we have to create enough buckets in the memory for every element, to place in the array.

Bucket Sort Time Complexity

Average case, best case, and worst case time complexity of this algorithm is O(n).

Related Tutorials

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.