Skip to content

Commit 93d7a4d

Browse files
author
AlexKlm
committed
Fix for test and code improvements
1 parent 50ccde3 commit 93d7a4d

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,44 @@
66
* BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
77
*/
88
public class BitonicSort implements SortAlgorithm {
9+
private enum Direction {
10+
DESCENDING,
11+
ASCENDING,
12+
}
913

1014
/**
1115
* Sorts the given array using the Bitonic Sort algorithm.
1216
*
1317
* @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
1519
* @return the sorted array
1620
*/
1721
@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;
2125
}
2226

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);
2529

2630
// 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);
2933

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);
3236
}
3337

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) {
3539
if (cnt > 1) {
3640
final int k = cnt / 2;
3741

3842
// Sort first half in ascending order
39-
bitonicSort(arr, low, k, true);
43+
bitonicSort(arr, low, k, Direction.ASCENDING);
4044

4145
// Sort second half in descending order
42-
bitonicSort(arr, low + k, cnt - k, false);
46+
bitonicSort(arr, low + k, cnt - k, Direction.DESCENDING);
4347

4448
// Merge the whole sequence in ascending order
4549
bitonicMerge(arr, low, cnt, dir);
@@ -55,12 +59,13 @@ private <T extends Comparable<T>> void bitonicSort(T[] arr, int low, int cnt, bo
5559
* @param cnt the number of elements in the sequence to be merged
5660
* @param dir the direction of sorting: true for ascending, false for descending
5761
*/
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) {
5963
if (cnt > 1) {
6064
final int k = cnt / 2;
6165

6266
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) {
6469
SortUtils.swap(arr, i, i + k);
6570
}
6671
}
@@ -91,4 +96,22 @@ private static int nextPowerOfTwo(int n) {
9196

9297
return 1 << count;
9398
}
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+
}
94117
}

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,4 @@ 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-
}
141119
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.thealgorithms.sorts;
22

33
public class BitonicSortTest extends SortingAlgorithmTest {
4-
private final BitonicSort bitonicSort = new BitonicSort();
5-
64
@Override
75
SortAlgorithm getSortAlgorithm() {
8-
return bitonicSort;
6+
return new BitonicSort();
97
}
108
}

0 commit comments

Comments
 (0)