-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBucket.java
More file actions
38 lines (38 loc) · 1.22 KB
/
Bucket.java
File metadata and controls
38 lines (38 loc) · 1.22 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
import java.util.*;
import java.time.*;
public class Bucket
{
static void bucketsort (float A[], int n)
{
@SuppressWarnings("unchecked")
ArrayList<Float>[] bkt = new ArrayList[n];
for (int i=0;i<n;i++)
bkt[i] = new ArrayList<Float>();
for (int i=0;i<n;i++)
{
int bktInd = (int)A[i]*n;
bkt[bktInd].add(A[i]);
}
for (int i=0;i<n;i++)
Collections.sort((bkt[i]));
// Here you can use any other sorting algorithm also...
int j=0;
for (int i=0;i<n;i++)
{
for (int k=0, size=bkt[i].size();k<size;k++)
A[j++] = bkt[i].get(k);
}
public static void main (String args[])
{
int n=10000;
float A[] = new float[n];
Random rnd = new Random();
for (int i=0;i<n;i++)
A[i] = rnd.nextFloat(10001);
double time1 = System.nanoTime();
bucketsort(A, n);
double time2 = System.nanoTime();
double timediff = (time2-time1)/1000; // in microseconds
System.out.println("\nThe Execution time taken for bucket sort algorithm is - " + timediff + "\n\n");
}
}