File tree 1 file changed +15
-0
lines changed
src/main/java/com/thealgorithms/sorts
1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
/**
4
4
* The WaveSort algorithm sorts an array so that every alternate element is greater than its adjacent elements.
5
+ * This implementation also provides a method to check if an array is wave sorted.
5
6
*/
6
7
public class WaveSort implements SortAlgorithm {
8
+ /**
9
+ * Sorts the given array such that every alternate element is greater than its adjacent elements.
10
+ *
11
+ * @param array The array to be sorted.
12
+ * @param <T> The type of elements in the array, which must be Comparable.
13
+ * @return The sorted array.
14
+ */
7
15
@ Override
8
16
public <T extends Comparable <T >> T [] sort (T [] array ) {
9
17
for (int i = 0 ; i < array .length ; i += 2 ) {
@@ -17,6 +25,13 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
17
25
return array ;
18
26
}
19
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.
30
+ *
31
+ * @param array The array to check.
32
+ * @param <T> The type of elements in the array, which must be Comparable.
33
+ * @return true if the array is wave sorted, false otherwise.
34
+ */
20
35
public <T extends Comparable <T >> boolean isWaveSorted (T [] array ) {
21
36
for (int i = 0 ; i < array .length ; i += 2 ) {
22
37
if (i > 0 && SortUtils .less (array [i ], array [i - 1 ])) {
You can’t perform that action at this time.
0 commit comments