11package Sorts ;
22
3+ import java .util .ArrayList ;
4+ import java .util .Collections ;
5+ import java .util .List ;
36import java .util .Random ;
4-
5- public class BucketSort
6- {
7- static int [] sort (int [] sequence , int maxValue )
8- {
9- // Bucket Sort
10- int [] Bucket = new int [maxValue + 1 ];
11- int [] sorted_sequence = new int [sequence .length ];
12-
13- for (int i = 0 ; i < sequence .length ; i ++)
14- Bucket [sequence [i ]]++;
15-
16- int outPos = 0 ;
17- for (int i = 0 ; i < Bucket .length ; i ++)
18- for (int j = 0 ; j < Bucket [i ]; j ++)
19- sorted_sequence [outPos ++] = i ;
20-
21- return sorted_sequence ;
7+
8+ /**
9+ * Wikipedia: https://en.wikipedia.org/wiki/Bucket_sort
10+ */
11+ public class BucketSort {
12+ public static void main (String [] args ) {
13+ int [] arr = new int [10 ];
14+
15+ /* generate 10 random numbers from -50 to 49 */
16+ Random random = new Random ();
17+ for (int i = 0 ; i < arr .length ; ++i ) {
18+ arr [i ] = random .nextInt (100 ) - 50 ;
19+ }
20+
21+ bucketSort (arr );
22+
23+ /* check array is sorted or not */
24+ for (int i = 0 , limit = arr .length - 1 ; i < limit ; ++i ) {
25+ assert arr [i ] <= arr [i + 1 ];
26+ }
2227 }
23-
24- static void printSequence (int [] sorted_sequence )
25- {
26- for (int i = 0 ; i < sorted_sequence .length ; i ++)
27- System .out .print (sorted_sequence [i ] + " " );
28+
29+ /**
30+ * BucketSort algorithms implements
31+ *
32+ * @param arr the array contains elements
33+ */
34+ private static void bucketSort (int [] arr ) {
35+ /* get max value of arr */
36+ int max = max (arr );
37+
38+ /* get min value of arr */
39+ int min = min (arr );
40+
41+ /* number of buckets */
42+ int numberOfBuckets = max - min + 1 ;
43+
44+ List <List <Integer >> buckets = new ArrayList <>(numberOfBuckets );
45+
46+ /* init buckets */
47+ for (int i = 0 ; i < numberOfBuckets ; ++i ) {
48+ buckets .add (new ArrayList <>());
49+ }
50+
51+ /* store elements to buckets */
52+ for (int value : arr ) {
53+ int hash = hash (value , max , numberOfBuckets );
54+ buckets .get (hash ).add (value );
55+ }
56+
57+ /* sort individual bucket */
58+ for (List <Integer > bucket : buckets ) {
59+ Collections .sort (bucket );
60+ }
61+
62+ /* concatenate buckets to origin array */
63+ int index = 0 ;
64+ for (List <Integer > bucket : buckets ) {
65+ for (int value : bucket ) {
66+ arr [index ++] = value ;
67+ }
68+ }
2869 }
29-
30- static int maxValue (int [] sequence )
31- {
32- int maxValue = 0 ;
33- for (int i = 0 ; i < sequence .length ; i ++)
34- if (sequence [i ] > maxValue )
35- maxValue = sequence [i ];
36- return maxValue ;
70+
71+
72+ /**
73+ * Get index of bucket which of our elements gets placed into it.
74+ *
75+ * @param elem the element of array to be sorted
76+ * @param min min value of array
77+ * @param numberOfBucket the number of bucket
78+ * @return index of bucket
79+ */
80+ private static int hash (int elem , int min , int numberOfBucket ) {
81+ return (elem - min ) / numberOfBucket ;
3782 }
38-
39- public static void main (String args [])
40- {
41- System .out .println ("Sorting of randomly generated numbers using BUCKET SORT" );
42- Random random = new Random ();
43- int N = 20 ;
44- int [] sequence = new int [N ];
45-
46- for (int i = 0 ; i < N ; i ++)
47- sequence [i ] = Math .abs (random .nextInt (100 ));
48-
49- int maxValue = maxValue (sequence );
50-
51- System .out .println ("\n Original Sequence: " );
52- printSequence (sequence );
53-
54- System .out .println ("\n Sorted Sequence: " );
55- printSequence (sort (sequence , maxValue ));
83+
84+ /**
85+ * Calculate max value of array
86+ *
87+ * @param arr the array contains elements
88+ * @return max value of given array
89+ */
90+ public static int max (int [] arr ) {
91+ int max = arr [0 ];
92+ for (int value : arr ) {
93+ if (value > max ) {
94+ max = value ;
95+ }
96+ }
97+ return max ;
98+ }
99+
100+ /**
101+ * Calculate min value of array
102+ *
103+ * @param arr the array contains elements
104+ * @return min value of given array
105+ */
106+ public static int min (int [] arr ) {
107+ int min = arr [0 ];
108+ for (int value : arr ) {
109+ if (value < min ) {
110+ min = value ;
111+ }
112+ }
113+ return min ;
56114 }
57- }
58-
59-
60- /*
61- #Output:
62-
63- $ javac Bucket_Sort.java
64- $ java Bucket_Sort
65-
66- Sorting of randomly generated numbers using BUCKET SORT
67-
68- Original Sequence:
69- 95 9 95 87 8 81 18 54 57 53 92 15 38 24 8 56 29 69 64 66
70- Sorted Sequence:
71- 8 8 9 15 18 24 29 38 53 54 56 57 64 66 69 81 87 92 95 95
72- */
115+ }
0 commit comments