-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathbucketsort.java
More file actions
30 lines (27 loc) · 880 Bytes
/
bucketsort.java
File metadata and controls
30 lines (27 loc) · 880 Bytes
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
// Bucket Sort in Java programming
import java.util.*;
class BucketSort {
public static void bucketSort(int[] array, int size) {
int max = (Arrays.stream(array).max().getAsInt());
int[] bucket = new int[max + 1];
for (int i = 0; i <= max; i++) {
bucket[i] = 0;
}
for (int i = 0; i < size; i++) {
bucket[array[i]]++;
}
for (int i = 0, j = 0; i <= max; i++) {
while (bucket[i] > 0) {
array[j++] = i;
bucket[i]--;
}
}
}
public static void main(String[] args) {
int[] data = { 4, 3, 4, 5, 6, 9, 1, 5 };
int size = data.length;
BucketSort bs = new BucketSort();
bs.bucketSort(data, size);
System.out.println("Sorted Array in Ascending Order: " + Arrays.toString(data));
}
}