You are on page 1of 8

Bucket Sort Algorithm: Overview, Time

Complexity, Pseudocode and Applications

The bucket sort algorithm is an advanced sorting technique, in contrast to others. Bucket sort can be
considered as a collective framework built using a variety of sorting algorithms.

For example, you can use one of the types of algorithms to sort elements into buckets and another
to sort elements within buckets. You could even recursively implement the algorithm you initially
chose to sort each bucket to minimize lines of code.

Because of this adaptability, bucket sorting becomes slightly more complex but also the most
versatile.

What Is a Bucket Sort Algorithm?


Bucket sort, also known as bin sort, is a sorting algorithm that divides an array's elements into
several buckets. The buckets are then sorted one at a time, either using a different sorting algorithm
or by recursively applying the bucket sorting algorithm.

The bucket sort method is as follows:

• Create an array of "buckets" that are initially empty

• Scatter: Go through the original array, placing each object in its appropriate bucket

• Each non-empty bucket should be sorted

• Gather: Return all elements to the original array after visiting the buckets in order

Moving ahead with this tutorial, you will now look at how the bucket sort algorithm works.
Working of a Bucket Sort Algorithm

The bucket sort algorithm works as follows.

• Assume the input array is:

If you take an integer as input, you must divide them by ten to obtain the floor value.

Make a one-dimensional array of size ten and fill it with zeros at first. Each array slot serves as a
bucket for storing elements.

• Insert array elements into the buckets. The elements are inserted following the bucket's
range.
The example code has buckets ranging from 0 to 1, 1 to 2, 2 to 3,.... (n-1) to n.

Assume that an input element with a value of.34 is chosen. It is multiplied by size = 10 (.34*10=3.4).
Then it is converted into an integer. Finally,.34 is inserted into bucket 3.

Similarly,0.39 is placed in the same bucket. Every time, the floating-point number's floor value is
taken.

• Each bucket's elements are sorted using one of the stable sorting algorithms.
• Each bucket's elements are gathered.

It is accomplished by iterating through the bucket and inserting one element at a time into the
original array. When an element from the bucket is copied into the original array, it is erased.

Pseudocode and an Algorithm of the Bucket Sort Algorithm

Pseudocode of the Bucket Sort


function bucket_sort (list, B) is // list is the name of the array

buckets <- new array of B empty lists

A <- the array's maximum key value

for i = 1 to length( list ) do

Insert array[i[ into buckets [ floor ( B * list[i] / A) ]

for i to B do

Sort buckets[i]

Return the gatttreation of buckets[1], buckets[2],...............buckets[B]

Let list denote the array to be sorted, and B the number of buckets used. The maximum key value in
linear time can be computed by iterating over all the keys once. Use the floor function to convert a
floating number to an integer. Sort is a sorting function that is used to order each bucket. In most
cases, insertion sort is used, but other algorithms, such as selection sort and merge sort, can also be
used.

Algorithm of the Bucket Sort

bucket _Sort_Algorithm ()

Make B buckets, each of which can store a range of values for all of the buckets.

Fill each bucket with 0 values for all buckets.

Put elements in buckets that match the range for all buckets.

Sort elements in each bucket and gather elements from each bucket

end bucket_Sort_Algorithm()

The Time Complexity of the Bucket Sort Algorithm

Bucket Sort's time complexity is largely determined by the size of the bucket list as well as the range
over which the array/element lists have been distributed.

Best Case Complexity O(n)

Average Case Complexity O(n)

Worst Case Complexity O(n*n)

Code Implementation of Bucket Sort Algorithm


#include <stdio.h>

#include<conio.h>

#include<stdlib.h>

int Max(int list[], int length)

int maximum = list[0];

for (int i = 1; i < length; i++)

if (list[i] > maximum)

maximum = list[i];

return maximum;

void bucketSortAlgorithm(int list[], int length)

int bucket[10];

const int maximum = Max(list, length);

for (int i = 0; i <= maximum; i++)


{

bucket[i] = 0;

for (int i = 0; i < length; i++)

bucket[list[i]]++;

for (int i = 0, j = 0; i <= maximum; i++)

while (bucket[i] > 0)

list[j++] = i;

bucket[i]--;

void display_Array(int list[], int length)


{

for (int i = 0; i < length; ++i)

printf("%d ", list[i]);

printf("\n");

int main()

int elements[] = {9, 5, 3, 5, 6, 7, 9, 4};

int length = sizeof(elements) / sizeof(elements[0]);

bucketSortAlgorithm(elements, length);

printf("After sorting in ascending order: \n");

display_Array(elements, length);

You might also like