Skip to content

Commit 2388655

Browse files
author
alx
committed
Moving max method to SortingUtils
1 parent b9430f2 commit 2388655

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

src/main/java/com/thealgorithms/sorts/BitonicSort.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
2020
return arr;
2121
}
2222

23-
int n = arr.length;
24-
int paddedSize = nextPowerOfTwo(n);
23+
final int paddedSize = nextPowerOfTwo(arr.length);
2524
T[] paddedArray = Arrays.copyOf(arr, paddedSize);
2625

2726
// 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);
3029

3130
bitonicSort(paddedArray, 0, paddedSize, true);
32-
33-
return Arrays.copyOf(paddedArray, n);
31+
return Arrays.copyOf(paddedArray, arr.length);
3432
}
3533

3634
private <T extends Comparable<T>> void bitonicSort(T[] arr, int low, int cnt, boolean dir) {
3735
if (cnt > 1) {
38-
int k = cnt / 2;
36+
final int k = cnt / 2;
3937

4038
// Sort first half in ascending order
4139
bitonicSort(arr, low, k, true);
@@ -50,7 +48,7 @@ private <T extends Comparable<T>> void bitonicSort(T[] arr, int low, int cnt, bo
5048

5149
private <T extends Comparable<T>> void bitonicMerge(T[] arr, int low, int cnt, boolean dir) {
5250
if (cnt > 1) {
53-
int k = cnt / 2;
51+
final int k = cnt / 2;
5452

5553
for (int i = low; i < low + k; i++) {
5654
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
6361
}
6462
}
6563

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-
8864
/**
8965
* Finds the next power of two greater than or equal to the given number.
9066
*

src/main/java/com/thealgorithms/sorts/SortUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,26 @@ public static <T extends Comparable<T>> boolean isSorted(List<T> list) {
116116
}
117117
return true;
118118
}
119+
120+
/**
121+
* Finds the maximum element in the given array.
122+
*
123+
* @param <T> the type of elements in the array, which must implement the Comparable interface
124+
* @param array the array to be searched
125+
* @return the maximum element in the array
126+
* @throws IllegalArgumentException if the array is null or empty
127+
*/
128+
public static <T extends Comparable<T>> T max(T[] array) {
129+
if (array == null || array.length == 0) {
130+
throw new IllegalArgumentException("Array must not be null or empty");
131+
}
132+
133+
T max = array[0];
134+
for (T element : array) {
135+
if (element.compareTo(max) > 0) {
136+
max = element;
137+
}
138+
}
139+
return max;
140+
}
119141
}

0 commit comments

Comments
 (0)