-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathBucketSort.java
45 lines (38 loc) · 1.28 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
// BucketSort.java
import java.util.ArrayList;
import java.util.Collections;
public class BucketSort {
public static void bucketSort(float[] array) {
int n = array.length;
if (n <= 0) return;
// Create empty buckets
ArrayList<Float>[] buckets = new ArrayList[n];
for (int i = 0; i < n; i++) {
buckets[i] = new ArrayList<>();
}
// Distribute elements into buckets
for (float value : array) {
int bucketIndex = (int) (value * n); // Assuming input in range [0, 1)
buckets[bucketIndex].add(value);
}
// Sort individual buckets
for (ArrayList<Float> bucket : buckets) {
Collections.sort(bucket);
}
// Concatenate buckets into the original array
int index = 0;
for (ArrayList<Float> bucket : buckets) {
for (float value : bucket) {
array[index++] = value;
}
}
}
public static void main(String[] args) {
float[] array = {0.42f, 0.32f, 0.53f, 0.12f, 0.66f, 0.90f};
bucketSort(array);
System.out.print("Sorted array: ");
for (float num : array) {
System.out.print(num + " ");
}
}
}