-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathBucketSort.java
More file actions
54 lines (47 loc) · 1.43 KB
/
BucketSort.java
File metadata and controls
54 lines (47 loc) · 1.43 KB
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
/*
* Bucket Sort Algorithm in Java
*
* Description:
* Bucket Sort works by distributing elements into buckets,
* sorting each bucket individually, then concatenating them.
*
* Time Complexity:
* Best: O(n+k), Average: O(n+k), Worst: O(n^2)
* Space Complexity:
* O(n + k) where k is number of buckets
*/
import java.util.*;
public class BucketSort {
public static void bucketSort(float[] arr) {
int n = arr.length;
if (n <= 0) return;
// 1) Create buckets
@SuppressWarnings("unchecked")
ArrayList<Float>[] buckets = new ArrayList[n];
for (int i = 0; i < n; i++) {
buckets[i] = new ArrayList<Float>();
}
// 2) Distribute array elements into buckets
for (float num : arr) {
int index = (int) (num * n);
buckets[index].add(num);
}
// 3) Sort individual buckets
for (ArrayList<Float> bucket : buckets) {
Collections.sort(bucket);
}
// 4) Concatenate buckets back into original array
int idx = 0;
for (ArrayList<Float> bucket : buckets) {
for (float num : bucket) {
arr[idx++] = num;
}
}
}
// Test code
public static void main(String[] args) {
float[] arr = {0.42f, 0.32f, 0.23f, 0.52f, 0.25f, 0.47f, 0.51f};
bucketSort(arr);
System.out.println(Arrays.toString(arr));
}
}