-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathBucketSort.java
56 lines (36 loc) · 1.01 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
import java.util.*;
class BucketSort {
static void bucketSort(int arr[], int n)
{
if (n <= 0)
return;
@SuppressWarnings("unchecked")
Vector<Float>[] buckets = new Vector[n];
for (int i = 0; i < n; i++) {
buckets[i] = new Vector<Float>();
}
for (int i = 0; i < n; i++) {
float idx = arr[i] * n;
buckets[(int)idx].add(arr[i]);
}
for (int i = 0; i < n; i++) {
Collections.sort(buckets[i]);
}
int index = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < buckets[i].size(); j++) {
arr[index++] = buckets[i].get(j);
}
}
}
public static void main(String args[])
{
int arr[] = {10,14,8,78,4,66,542,22,14};
int n = arr.length;
bucketSort(arr, n);
System.out.println("Sorted array is ");
for (int el : arr) {
System.out.print(el + " ");
}
}
}