Bucket Sort Algorithm
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].
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
4. for i ← 0 to n-1
5. do sort list B [i] with insertion sort.
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()
end bucketSort
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);
}
}
}