forked from rahul22mrk/hackoctoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket.java
More file actions
49 lines (36 loc) · 828 Bytes
/
bucket.java
File metadata and controls
49 lines (36 loc) · 828 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
class bucket {
static void bucketSort(float arr[], int n)
{
if (n <= 0)
return;
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[])
{
float arr[] = { (float)0.3897, (float)0.5654,
(float)0.6356, (float)0.14234,
(float)0.675665, (float)0.34934 };
int n = arr.length;
bucketSort(arr, n);
for (float el : arr) {
System.out.print(el + " ");
}
}
}