@@ -20,22 +20,20 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
20
20
return arr ;
21
21
}
22
22
23
- int n = arr .length ;
24
- int paddedSize = nextPowerOfTwo (n );
23
+ final int paddedSize = nextPowerOfTwo (arr .length );
25
24
T [] paddedArray = Arrays .copyOf (arr , paddedSize );
26
25
27
26
// Fill the padded part with a maximum value
28
- T maxValue = findMax (arr );
29
- Arrays .fill (paddedArray , n , paddedSize , maxValue );
27
+ final T maxValue = SortUtils . max (arr );
28
+ Arrays .fill (paddedArray , arr . length , paddedSize , maxValue );
30
29
31
30
bitonicSort (paddedArray , 0 , paddedSize , true );
32
-
33
- return Arrays .copyOf (paddedArray , n );
31
+ return Arrays .copyOf (paddedArray , arr .length );
34
32
}
35
33
36
34
private <T extends Comparable <T >> void bitonicSort (T [] arr , int low , int cnt , boolean dir ) {
37
35
if (cnt > 1 ) {
38
- int k = cnt / 2 ;
36
+ final int k = cnt / 2 ;
39
37
40
38
// Sort first half in ascending order
41
39
bitonicSort (arr , low , k , true );
@@ -50,7 +48,7 @@ private <T extends Comparable<T>> void bitonicSort(T[] arr, int low, int cnt, bo
50
48
51
49
private <T extends Comparable <T >> void bitonicMerge (T [] arr , int low , int cnt , boolean dir ) {
52
50
if (cnt > 1 ) {
53
- int k = cnt / 2 ;
51
+ final int k = cnt / 2 ;
54
52
55
53
for (int i = low ; i < low + k ; i ++) {
56
54
if (dir == (arr [i ].compareTo (arr [i + k ]) > 0 )) {
@@ -63,28 +61,6 @@ private <T extends Comparable<T>> void bitonicMerge(T[] arr, int low, int cnt, b
63
61
}
64
62
}
65
63
66
- /**
67
- * Finds the maximum element in the given array.
68
- *
69
- * @param <T> the type of elements in the array, which must implement the Comparable interface
70
- * @param array the array to be searched
71
- * @return the maximum element in the array
72
- * @throws IllegalArgumentException if the array is null or empty
73
- */
74
- public static <T extends Comparable <T >> T findMax (T [] array ) {
75
- if (array == null || array .length == 0 ) {
76
- throw new IllegalArgumentException ("Array must not be null or empty" );
77
- }
78
-
79
- T max = array [0 ];
80
- for (T element : array ) {
81
- if (element .compareTo (max ) > 0 ) {
82
- max = element ;
83
- }
84
- }
85
- return max ;
86
- }
87
-
88
64
/**
89
65
* Finds the next power of two greater than or equal to the given number.
90
66
*
0 commit comments