Skip to content

Commit 9fbc959

Browse files
authored
Merge pull request #93 from harshalkh/patch-1
Create BucketSort.java
2 parents ddc8a74 + 08f6b22 commit 9fbc959

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

BucketSort.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
import java.util.*;
3+
import java.util.Collections;
4+
5+
class BucketSort {
6+
7+
static void bucketSort(float arr[], int n)
8+
{
9+
if (n <= 0)
10+
return;
11+
12+
Vector<Float>[] buckets = new Vector[n];
13+
14+
for (int i = 0; i < n; i++) {
15+
buckets[i] = new Vector<Float>();
16+
}
17+
18+
// Put array elements in different buckets
19+
for (int i = 0; i < n; i++) {
20+
float idx = arr[i] * n;
21+
buckets[(int)idx].add(arr[i]);
22+
}
23+
24+
// Sort individual buckets
25+
for (int i = 0; i < n; i++) {
26+
Collections.sort(buckets[i]);
27+
}
28+
29+
// Concatenate all buckets
30+
int index = 0;
31+
for (int i = 0; i < n; i++) {
32+
for (int j = 0; j < buckets[i].size(); j++) {
33+
arr[index++] = buckets[i].get(j);
34+
}
35+
}
36+
}
37+
38+
public static void main(String args[])
39+
{
40+
float arr[] = { (float)0.89, (float)0.55,
41+
(float)0.65, (float)0.124,
42+
(float)0.66, (float)0.34 };
43+
44+
int n = arr.length;
45+
bucketSort(arr, n);
46+
47+
System.out.println("Sorted array is ");
48+
for (float el : arr) {
49+
System.out.print(el + " ");
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)