-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
BucketSort.java
72 lines (62 loc) · 2.16 KB
/
BucketSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Bucket sort implementation
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.BucketSort
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.sorting;
import java.util.*;
public class BucketSort implements InplaceSort {
@Override
public void sort(int[] values) {
int minValue = Integer.MAX_VALUE;
int maxValue = Integer.MIN_VALUE;
for (int i = 0; i < values.length; i++) {
if (values[i] < minValue) minValue = values[i];
if (values[i] > maxValue) maxValue = values[i];
}
BucketSort.bucketSort(values, minValue, maxValue);
}
// Performs a bucket sort of an array in which all the elements are
// bounded in the range [minValue, maxValue]. For bucket sort to give linear
// performance the elements need to be uniformly distributed
private static void bucketSort(int[] ar, int minValue, int maxValue) {
if (ar == null || ar.length == 0 || minValue == maxValue) return;
// N is number elements and M is the range of values
final int N = ar.length, M = maxValue - minValue + 1, numBuckets = M / N + 1;
List<List<Integer>> buckets = new ArrayList<>(numBuckets);
for (int i = 0; i < numBuckets; i++) buckets.add(new ArrayList<>());
// Place each element in a bucket
for (int i = 0; i < N; i++) {
int bi = (ar[i] - minValue) / M;
List<Integer> bucket = buckets.get(bi);
bucket.add(ar[i]);
}
// Sort buckets and stitch together answer
for (int bi = 0, j = 0; bi < numBuckets; bi++) {
List<Integer> bucket = buckets.get(bi);
if (bucket != null) {
Collections.sort(bucket);
for (int k = 0; k < bucket.size(); k++) {
ar[j++] = bucket.get(k);
}
}
}
}
public static void main(String[] args) {
BucketSort sorter = new BucketSort();
int[] array = {10, 4, 6, 8, 13, 2, 3};
sorter.sort(array);
// Prints:
// [2, 3, 4, 6, 8, 10, 13]
System.out.println(java.util.Arrays.toString(array));
array = new int[] {10, 10, 10, 10, 10};
sorter.sort(array);
// Prints:
// [10, 10, 10, 10, 10]
System.out.println(java.util.Arrays.toString(array));
}
}