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

Method sort

src/main/java/com/thealgorithms/sorts/WaveSort.java:15–26  ·  view source on GitHub ↗

Sorts the given array such that every alternate element is greater than its adjacent elements. @param array The array to be sorted. @param The type of elements in the array, which must be Comparable. @return The sorted array.

(T[] array)

Source from the content-addressed store, hash-verified

13 * @return The sorted array.
14 */
15 @Override
16 public <T extends Comparable<T>> T[] sort(T[] array) {
17 for (int i = 0; i < array.length; i += 2) {
18 if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
19 SortUtils.swap(array, i, i - 1);
20 }
21 if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
22 SortUtils.swap(array, i, i + 1);
23 }
24 }
25 return array;
26 }
27
28 /**
29 * Checks if the given array is wave sorted. An array is wave sorted if every alternate element is greater than its adjacent elements.

Callers 1

waveSortTestMethod · 0.95

Calls 2

lessMethod · 0.95
swapMethod · 0.95

Tested by 1

waveSortTestMethod · 0.76