MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / sort

Method sort

src/main/java/com/thealgorithms/sorts/BitonicSort.java:22–37  ·  view source on GitHub ↗

Sorts the given array using the Bitonic Sort algorithm. @param the type of elements in the array, which must implement the Comparable interface @param array the array to be sorted @return the sorted array

(T[] array)

Source from the content-addressed store, hash-verified

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) {

Callers

nothing calls this directly

Calls 4

nextPowerOfTwoMethod · 0.95
maxMethod · 0.95
bitonicSortMethod · 0.95
fillMethod · 0.80

Tested by

no test coverage detected