Skip to content

Commit d87dad6

Browse files
committed
Added formatting
1 parent 0071d5a commit d87dad6

11 files changed

+629
-610
lines changed
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
package com.thealgorithms.sorts;
22

33
public class AdaptiveMergeSort implements SortAlgorithm {
4-
@SuppressWarnings("unchecked")
5-
public <T extends Comparable<T>> T[] sort(T[] array) {
6-
if (array.length <= 1) {
7-
return array;
8-
}
9-
T[] aux = array.clone();
10-
sort(array, aux, 0, array.length - 1);
11-
return array;
12-
}
4+
@SuppressWarnings("unchecked")
5+
public <T extends Comparable<T>> T[] sort(T[] array) {
6+
if (array.length <= 1) {
7+
return array;
8+
}
9+
T[] aux = array.clone();
10+
sort(array, aux, 0, array.length - 1);
11+
return array;
12+
}
1313

14-
private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) {
15-
if (low >= high) {
16-
return;
17-
}
18-
int mid = low + (high - low) / 2;
19-
sort(array, aux, low, mid);
20-
sort(array, aux, mid + 1, high);
21-
merge(array, aux, low, mid, high);
22-
}
14+
private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) {
15+
if (low >= high) {
16+
return;
17+
}
18+
int mid = low + (high - low) / 2;
19+
sort(array, aux, low, mid);
20+
sort(array, aux, mid + 1, high);
21+
merge(array, aux, low, mid, high);
22+
}
2323

24-
private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) {
25-
System.arraycopy(array, low, aux, low, high - low + 1);
26-
int i = low;
27-
int j = mid + 1;
28-
for (int k = low; k <= high; k++) {
29-
if (i > mid) {
30-
array[k] = aux[j++];
31-
} else if (j > high) {
32-
array[k] = aux[i++];
33-
} else if (SortUtils.less(aux[j],aux[i])) {
34-
array[k] = aux[j++];
35-
} else {
36-
array[k] = aux[i++];
37-
}
38-
}
39-
}
24+
private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) {
25+
System.arraycopy(array, low, aux, low, high - low + 1);
26+
int i = low;
27+
int j = mid + 1;
28+
for (int k = low; k <= high; k++) {
29+
if (i > mid) {
30+
array[k] = aux[j++];
31+
} else if (j > high) {
32+
array[k] = aux[i++];
33+
} else if (SortUtils.less(aux[j], aux[i])) {
34+
array[k] = aux[j++];
35+
} else {
36+
array[k] = aux[i++];
37+
}
38+
}
39+
}
4040
}

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

Lines changed: 107 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,109 +4,113 @@
44
import java.util.function.BiPredicate;
55

66
/**
7-
* BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
7+
* BitonicSort class implements the SortAlgorithm interface using the bitonic
8+
* sort technique.
89
*/
910
public class BitonicSort implements SortAlgorithm {
10-
private enum Direction {
11-
DESCENDING,
12-
ASCENDING,
13-
}
14-
15-
/**
16-
* Sorts the given array using the Bitonic Sort algorithm.
17-
*
18-
* @param <T> the type of elements in the array, which must implement the Comparable interface
19-
* @param array the array to be sorted
20-
* @return the sorted array
21-
*/
22-
@Override
23-
public <T extends Comparable<T>> T[] sort(T[] array) {
24-
if (array.length == 0) {
25-
return array;
26-
}
27-
28-
final int paddedSize = nextPowerOfTwo(array.length);
29-
T[] paddedArray = Arrays.copyOf(array, paddedSize);
30-
31-
// Fill the padded part with a maximum value
32-
final T maxValue = max(array);
33-
Arrays.fill(paddedArray, array.length, paddedSize, maxValue);
34-
35-
bitonicSort(paddedArray, 0, paddedSize, Direction.ASCENDING);
36-
return Arrays.copyOf(paddedArray, array.length);
37-
}
38-
39-
private <T extends Comparable<T>> void bitonicSort(final T[] array, final int low, final int cnt, final Direction direction) {
40-
if (cnt > 1) {
41-
final int k = cnt / 2;
42-
43-
// Sort first half in ascending order
44-
bitonicSort(array, low, k, Direction.ASCENDING);
45-
46-
// Sort second half in descending order
47-
bitonicSort(array, low + k, cnt - k, Direction.DESCENDING);
48-
49-
// Merge the whole sequence in ascending order
50-
bitonicMerge(array, low, cnt, direction);
51-
}
52-
}
53-
54-
/**
55-
* Merges the bitonic sequence in the specified direction.
56-
*
57-
* @param <T> the type of elements in the array, which must be Comparable
58-
* @param array the array containing the bitonic sequence to be merged
59-
* @param low the starting index of the sequence to be merged
60-
* @param cnt the number of elements in the sequence to be merged
61-
* @param direction the direction of sorting
62-
*/
63-
private <T extends Comparable<T>> void bitonicMerge(T[] array, int low, int cnt, Direction direction) {
64-
if (cnt > 1) {
65-
final int k = cnt / 2;
66-
67-
final BiPredicate<T, T> areSorted = (direction == Direction.ASCENDING) ? (a, b) -> SortUtils.less(a,b) : (a, b) -> SortUtils.greater(a,b);
68-
for (int i = low; i < low + k; i++) {
69-
if (!areSorted.test(array[i], array[i + k])) {
70-
SortUtils.swap(array, i, i + k);
71-
}
72-
}
73-
74-
bitonicMerge(array, low, k, direction);
75-
bitonicMerge(array, low + k, cnt - k, direction);
76-
}
77-
}
78-
79-
/**
80-
* Finds the next power of two greater than or equal to the given number.
81-
*
82-
* @param n the number
83-
* @return the next power of two
84-
*/
85-
private static int nextPowerOfTwo(int n) {
86-
int count = 0;
87-
88-
// First n in the below condition is for the case where n is 0
89-
if ((n & (n - 1)) == 0) {
90-
return n;
91-
}
92-
93-
while (n != 0) {
94-
n >>= 1;
95-
count += 1;
96-
}
97-
98-
return 1 << count;
99-
}
100-
101-
/**
102-
* Finds the maximum element in the given array.
103-
*
104-
* @param <T> the type of elements in the array, which must implement the Comparable interface
105-
* @param array the array to be searched
106-
* @return the maximum element in the array
107-
* @throws IllegalArgumentException if the array is null or empty
108-
*/
109-
private static <T extends Comparable<T>> T max(final T[] array) {
110-
return Arrays.stream(array).max(Comparable::compareTo).orElseThrow();
111-
}
11+
private enum Direction {
12+
DESCENDING, ASCENDING,
13+
}
14+
15+
/**
16+
* Sorts the given array using the Bitonic Sort algorithm.
17+
*
18+
* @param <T> the type of elements in the array, which must implement the
19+
* Comparable interface
20+
* @param array the array to be sorted
21+
* @return the sorted array
22+
*/
23+
@Override
24+
public <T extends Comparable<T>> T[] sort(T[] array) {
25+
if (array.length == 0) {
26+
return array;
27+
}
28+
29+
final int paddedSize = nextPowerOfTwo(array.length);
30+
T[] paddedArray = Arrays.copyOf(array, paddedSize);
31+
32+
// Fill the padded part with a maximum value
33+
final T maxValue = max(array);
34+
Arrays.fill(paddedArray, array.length, paddedSize, maxValue);
35+
36+
bitonicSort(paddedArray, 0, paddedSize, Direction.ASCENDING);
37+
return Arrays.copyOf(paddedArray, array.length);
38+
}
39+
40+
private <T extends Comparable<T>> void bitonicSort(final T[] array, final int low, final int cnt,
41+
final Direction direction) {
42+
if (cnt > 1) {
43+
final int k = cnt / 2;
44+
45+
// Sort first half in ascending order
46+
bitonicSort(array, low, k, Direction.ASCENDING);
47+
48+
// Sort second half in descending order
49+
bitonicSort(array, low + k, cnt - k, Direction.DESCENDING);
50+
51+
// Merge the whole sequence in ascending order
52+
bitonicMerge(array, low, cnt, direction);
53+
}
54+
}
55+
56+
/**
57+
* Merges the bitonic sequence in the specified direction.
58+
*
59+
* @param <T> the type of elements in the array, which must be Comparable
60+
* @param array the array containing the bitonic sequence to be merged
61+
* @param low the starting index of the sequence to be merged
62+
* @param cnt the number of elements in the sequence to be merged
63+
* @param direction the direction of sorting
64+
*/
65+
private <T extends Comparable<T>> void bitonicMerge(T[] array, int low, int cnt, Direction direction) {
66+
if (cnt > 1) {
67+
final int k = cnt / 2;
68+
69+
final BiPredicate<T, T> areSorted = (direction == Direction.ASCENDING) ? (a, b) -> SortUtils.less(a, b)
70+
: (a, b) -> SortUtils.greater(a, b);
71+
for (int i = low; i < low + k; i++) {
72+
if (!areSorted.test(array[i], array[i + k])) {
73+
SortUtils.swap(array, i, i + k);
74+
}
75+
}
76+
77+
bitonicMerge(array, low, k, direction);
78+
bitonicMerge(array, low + k, cnt - k, direction);
79+
}
80+
}
81+
82+
/**
83+
* Finds the next power of two greater than or equal to the given number.
84+
*
85+
* @param n the number
86+
* @return the next power of two
87+
*/
88+
private static int nextPowerOfTwo(int n) {
89+
int count = 0;
90+
91+
// First n in the below condition is for the case where n is 0
92+
if ((n & (n - 1)) == 0) {
93+
return n;
94+
}
95+
96+
while (n != 0) {
97+
n >>= 1;
98+
count += 1;
99+
}
100+
101+
return 1 << count;
102+
}
103+
104+
/**
105+
* Finds the maximum element in the given array.
106+
*
107+
* @param <T> the type of elements in the array, which must implement the
108+
* Comparable interface
109+
* @param array the array to be searched
110+
* @return the maximum element in the array
111+
* @throws IllegalArgumentException if the array is null or empty
112+
*/
113+
private static <T extends Comparable<T>> T max(final T[] array) {
114+
return Arrays.stream(array).max(Comparable::compareTo).orElseThrow();
115+
}
112116
}

0 commit comments

Comments
 (0)