0% found this document useful (0 votes)
45 views

Bucket Sort Algorithm

The bucket sort algorithm sorts elements by distributing them into buckets based on their value ranges. Elements within each bucket are then sorted using another sorting algorithm and the sorted buckets are concatenated together to produce the fully sorted output array. It works by creating buckets, distributing the elements into the correctly ranged buckets, sorting each bucket, and gathering the sorted elements out of the buckets.

Uploaded by

SUCHARIKA MAHAT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Bucket Sort Algorithm

The bucket sort algorithm sorts elements by distributing them into buckets based on their value ranges. Elements within each bucket are then sorted using another sorting algorithm and the sorted buckets are concatenated together to produce the fully sorted output array. It works by creating buckets, distributing the elements into the correctly ranged buckets, sorting each bucket, and gathering the sorted elements out of the buckets.

Uploaded by

SUCHARIKA MAHAT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Bucket Sort Algorithm

Bucket Sort is a sorting technique that sorts the elements by first dividing the elements into
several groups called buckets. The elements inside each bucket are sorted using any of the
suitable sorting algorithms or recursively calling the same algorithm.

Several buckets are created. Each bucket is filled with a specific range of elements. The elements
inside the bucket are sorted using any other algorithm. Finally, the elements of the bucket are
gathered to get the sorted array.

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

Bucket Sort runs in linear time on average. Like Counting Sort, bucket Sort is fast because it
considers something about the input. Bucket Sort considers that the input is generated by a
random process that distributes elements uniformly over the intervalμ=[0,1].

To sort n input numbers, Bucket Sort


1. Partition μ into n non-overlapping intervals called buckets.
2. Puts each input number into its buckets
3. Sort each bucket using a simple algorithm, e.g. Insertion Sort and then
4. Concatenates the sorted lists.

Bucket Sort considers that the input is an n element array A and that each element A [i] in the
array satisfies 0≤A [i] <1. The code depends upon an auxiliary array B [0....n-1] of linked lists
(buckets) and considers that there is a mechanism for maintaining such lists.

BUCKET-SORT (A)

1. n ← length [A]

2. for i ← 1 to n

3. do insert A [i] into list B [n A[i]]

4. for i ← 0 to n-1
5. do sort list B [i] with insertion sort.

6. Concatenate the lists B [0], B [1] ...B [n-1] together in order.

Example:

A = (0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68)  

Bucket Sort
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

Program for Bucket sort


import java.util.*;

public class BucketSort {


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

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


bucket[i] = new ArrayList<Float>();

for (int i = 0; i < n; i++) {


int bucketIndex = (int) arr[i] * n;
bucket[bucketIndex].add(arr[i]);
}

for (int i = 0; i < n; i++) {


Collections.sort((bucket[i]));
}

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);
}
}
}

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 + " ");
}
}

You might also like