6
6
* BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
7
7
*/
8
8
public class BitonicSort implements SortAlgorithm {
9
+ private enum Direction {
10
+ DESCENDING ,
11
+ ASCENDING ,
12
+ }
9
13
10
14
/**
11
15
* Sorts the given array using the Bitonic Sort algorithm.
12
16
*
13
17
* @param <T> the type of elements in the array, which must implement the Comparable interface
14
- * @param arr the array to be sorted
18
+ * @param unsorted the array to be sorted
15
19
* @return the sorted array
16
20
*/
17
21
@ Override
18
- public <T extends Comparable <T >> T [] sort (T [] arr ) {
19
- if (arr == null || arr .length == 0 ) {
20
- return arr ;
22
+ public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
23
+ if (unsorted == null || unsorted .length == 0 ) {
24
+ return unsorted ;
21
25
}
22
26
23
- final int paddedSize = nextPowerOfTwo (arr .length );
24
- T [] paddedArray = Arrays .copyOf (arr , paddedSize );
27
+ final int paddedSize = nextPowerOfTwo (unsorted .length );
28
+ T [] paddedArray = Arrays .copyOf (unsorted , paddedSize );
25
29
26
30
// Fill the padded part with a maximum value
27
- final T maxValue = SortUtils . max (arr );
28
- Arrays .fill (paddedArray , arr .length , paddedSize , maxValue );
31
+ final T maxValue = max (unsorted );
32
+ Arrays .fill (paddedArray , unsorted .length , paddedSize , maxValue );
29
33
30
- bitonicSort (paddedArray , 0 , paddedSize , true );
31
- return Arrays .copyOf (paddedArray , arr .length );
34
+ bitonicSort (paddedArray , 0 , paddedSize , Direction . ASCENDING );
35
+ return Arrays .copyOf (paddedArray , unsorted .length );
32
36
}
33
37
34
- private <T extends Comparable <T >> void bitonicSort (T [] arr , int low , int cnt , boolean dir ) {
38
+ private <T extends Comparable <T >> void bitonicSort (T [] arr , int low , int cnt , Direction dir ) {
35
39
if (cnt > 1 ) {
36
40
final int k = cnt / 2 ;
37
41
38
42
// Sort first half in ascending order
39
- bitonicSort (arr , low , k , true );
43
+ bitonicSort (arr , low , k , Direction . ASCENDING );
40
44
41
45
// Sort second half in descending order
42
- bitonicSort (arr , low + k , cnt - k , false );
46
+ bitonicSort (arr , low + k , cnt - k , Direction . DESCENDING );
43
47
44
48
// Merge the whole sequence in ascending order
45
49
bitonicMerge (arr , low , cnt , dir );
@@ -55,12 +59,13 @@ private <T extends Comparable<T>> void bitonicSort(T[] arr, int low, int cnt, bo
55
59
* @param cnt the number of elements in the sequence to be merged
56
60
* @param dir the direction of sorting: true for ascending, false for descending
57
61
*/
58
- private <T extends Comparable <T >> void bitonicMerge (T [] arr , int low , int cnt , boolean dir ) {
62
+ private <T extends Comparable <T >> void bitonicMerge (T [] arr , int low , int cnt , Direction dir ) {
59
63
if (cnt > 1 ) {
60
64
final int k = cnt / 2 ;
61
65
62
66
for (int i = low ; i < low + k ; i ++) {
63
- if (dir == (arr [i ].compareTo (arr [i + k ]) > 0 )) {
67
+ boolean condition = (dir == Direction .ASCENDING ) ? arr [i ].compareTo (arr [i + k ]) > 0 : arr [i ].compareTo (arr [i + k ]) < 0 ;
68
+ if (condition ) {
64
69
SortUtils .swap (arr , i , i + k );
65
70
}
66
71
}
@@ -91,4 +96,22 @@ private static int nextPowerOfTwo(int n) {
91
96
92
97
return 1 << count ;
93
98
}
99
+
100
+ /**
101
+ * Finds the maximum element in the given array.
102
+ *
103
+ * @param <T> the type of elements in the array, which must implement the Comparable interface
104
+ * @param array the array to be searched
105
+ * @return the maximum element in the array
106
+ * @throws IllegalArgumentException if the array is null or empty
107
+ */
108
+ private static <T extends Comparable <T >> T max (T [] array ) {
109
+ T max = array [0 ];
110
+ for (T element : array ) {
111
+ if (SortUtils .greater (element , max )) {
112
+ max = element ;
113
+ }
114
+ }
115
+ return max ;
116
+ }
94
117
}
0 commit comments