Dealing with Bucket sort

shankar sravanth
3 min readOct 4, 2021

--

Bucket sort, or bin sort implementation, algorithm, code (java, python)

‘a sorting algorithm that works by distributing the elements of an array into a number of buckets’

Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. Sort each non-empty bucket.

BUCKET SORT (Process)

Scatter-gather approach:

The process of bucket sort can be understood as a scatter-gather approach. Here, elements are first scattered into buckets then the elements in each bucket are sorted. Finally, the elements are gathered in order.

ALGORITHM:

bucketSort()
create N buckets each of which can hold a range of values
for all the buckets
initialize each bucket with 0 values
for all the buckets
put elements into buckets matching the range
for all the buckets
sort elements in each bucket
gather elements from each bucket
end bucketSort

Code in java:

import java.util.ArrayList;
import java.util.Collections;

public class BucketSort {
public void bucketSort(float[] arr, int n) {
if (n <= 0)
return;
@SuppressWarnings("unchecked")
ArrayList<Float>[] bucket = new ArrayList[n];

// Create empty buckets
for (int i = 0; i < n; i++)
bucket[i] = new ArrayList<Float>();

// Add elements into the buckets
for (int i = 0; i < n; i++) {
int bucketIndex = (int) arr[i] * n;
bucket[bucketIndex].add(arr[i]);
}

// Sort the elements of each bucket
for (int i = 0; i < n; i++) {
Collections.sort((bucket[i]));
}

// Get the sorted array
int index = 0;
for (int i = 0; i < n; i++) {
for (int j = 0, size = bucket[i].size(); j < size; j++) {
arr[index++] = bucket[i].get(j);
}
}
}

// Driver code
public static void main(String[] args) {
BucketSort b = new BucketSort();
float[] arr = { (float) 0.42, (float) 0.32, (float) 0.33, (float) 0.52, (float) 0.37, (float) 0.47,
(float) 0.51 };
b.bucketSort(arr, 7);

for (float i : arr)
System.out.print(i + " ");
}
}

Code in Python:

def bucketSort(array):
bucket = []

# Create empty buckets
for i in range(len(array)):
bucket.append([])

# Insert elements into their respective buckets
for j in array:
index_b = int(10 * j)
bucket[index_b].append(j)

# Sort the elements of each bucket
for i in range(len(array)):
bucket[i] = sorted(bucket[i])

# Get the sorted elements
k = 0
for i in range(len(array)):
for j in range(len(bucket[i])):
array[k] = bucket[i][j]
k += 1
return array


array = [.42, .32, .33, .52, .37, .47, .51]
print("Sorted Array in descending order is")
print(bucketSort(array))

Time and space complexity :

Time Complexity

BestO→(n+k)

Worst →O(n^2)

Average →O(n)

Space Complexity →O(n+k)

Bucket Sort Applications

Bucket sort is used when:

1. input is uniformly distributed over a range.

2. there are floating point values

Advantages of bucket sort:

  1. One of the main advantages of a bucket sort is that is quicker to run than a bubble sort. Putting data into small buckets that can be sorted individually reduces the number of comparisons that need to be carried out.
  2. Bucket sort can be exceptionally fast because of the way elements are assigned to buckets, typically using an array where the index is the value. This means that more auxiliary memory is required for the buckets at the cost of running time than more comparison sorts

--

--

No responses yet