forked from keshavagarwal17/All-Cp-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucketSort.java
50 lines (36 loc) · 1.16 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
import java.util.*;
public class BucketSort {
public static void main(String args[]) {
Random rand = new Random();
int size=6000;
float[] a = new float[size];
double start,duration;
for (int i = 0; i < size; i++) {
a[i] = rand.nextFloat();
}
start = System.nanoTime();
bucketsort(a, size);
duration = (System.nanoTime() - start)/1000.0;
System.out.println("Time is "+ duration);
}
static void bucketsort(float a[], int size) {
@SuppressWarnings("unchecked")
ArrayList<Float>[] b = new ArrayList[size];
for (int i = 0; i < size; i++) {
b[i] = new ArrayList<Float>();
}
for (int i = 0; i < size; i++) {
int index = (int) (a[i]*size);
b[index].add(a[i]);
}
for (int i = 0; i < size; i++) {
Collections.sort(b[i]);
}
int index1 = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < b[i].size(); j++) {
a[index1++] = b[i].get(j);
}
}
}
}