|
4 | 4 | import java.util.function.BiPredicate;
|
5 | 5 |
|
6 | 6 | /**
|
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. |
8 | 9 | */
|
9 | 10 | 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 | + } |
112 | 116 | }
|
0 commit comments