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