File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments