Skip to content

Commit 38926cc

Browse files
authored
Merge branch 'master' into implementation/selection_sort_recursive
2 parents 1636862 + 7054535 commit 38926cc

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@
529529
* [TopologicalSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/TopologicalSort.java)
530530
* [TreeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/TreeSort.java)
531531
* [WiggleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/WiggleSort.java)
532+
* [WaveSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/WaveSort.java)
532533
* stacks
533534
* [BalancedBrackets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java)
534535
* [DecimalToAnyUsingStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java)
@@ -887,6 +888,7 @@
887888
* [TopologicalSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/TopologicalSortTest.java)
888889
* [TreeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/TreeSortTest.java)
889890
* [WiggleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/WiggleSortTest.java)
891+
* [WaveSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/WaveSortTest.java)
890892
* stacks
891893
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)
892894
* strings
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.sorts;
2+
3+
/**
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.
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+
*/
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.
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+
*/
35+
public <T extends Comparable<T>> boolean isWaveSorted(T[] array) {
36+
for (int i = 0; i < array.length; i += 2) {
37+
if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
38+
return false;
39+
}
40+
if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
41+
return false;
42+
}
43+
}
44+
return true;
45+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.stream.Stream;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class WaveSortTest {
13+
@ParameterizedTest
14+
@MethodSource("arraysToWaveSort")
15+
public void waveSortTest(Integer[] array) {
16+
WaveSort waveSort = new WaveSort();
17+
final var inputHistogram = getHistogram(array);
18+
final var sortedArray = waveSort.sort(array);
19+
assertTrue(waveSort.isWaveSorted(sortedArray));
20+
final var sortedHistogram = getHistogram(sortedArray);
21+
assertEquals(inputHistogram, sortedHistogram, "Element counts do not match");
22+
}
23+
24+
private Map<Integer, Integer> getHistogram(Integer[] array) {
25+
Map<Integer, Integer> histogram = new HashMap<>();
26+
for (final var element : array) {
27+
histogram.put(element, histogram.getOrDefault(element, 0) + 1);
28+
}
29+
return histogram;
30+
}
31+
32+
private static Stream<Object[]> arraysToWaveSort() {
33+
return Stream.of(new Object[] {new Integer[] {7, 7, 11, 3, 4, 5, 15}}, new Object[] {new Integer[] {1, 2, 3, 4, 5, 6, 7, 8}}, new Object[] {new Integer[] {8, 7, 6, 5, 4, 3, 2, 1}}, new Object[] {new Integer[] {3, 3, 3, 3}}, new Object[] {new Integer[] {-1, -3, -2, -4, -6, -5}},
34+
new Object[] {new Integer[] {5, 3, 1, 2, 9, 7, 6, 8, 4, 0}}, new Object[] {new Integer[] {1}}, new Object[] {new Integer[] {2, 1}}, new Object[] {new Integer[] {1, 2}}, new Object[] {new Integer[] {}}, new Object[] {new Integer[] {0, 5, -3, 2, -1, 4, -2, 1, 3}});
35+
}
36+
37+
@ParameterizedTest
38+
@MethodSource("waveSortedArrays")
39+
public <T extends Comparable<T>> void testIsWaveSorted(T[] array, boolean expected) {
40+
final WaveSort waveSort = new WaveSort();
41+
assertEquals(expected, waveSort.isWaveSorted(array));
42+
}
43+
public static Stream<Object[]> waveSortedArrays() {
44+
return Stream.of(new Object[] {new Integer[] {3, 1, 4, 2, 5}, Boolean.TRUE}, new Object[] {new Integer[] {3, 1, 4, 2}, Boolean.TRUE}, new Object[] {new Integer[] {1, 3, 2, 4, 5}, Boolean.FALSE}, new Object[] {new Integer[] {4, 3, 5, 2, 3, 1, 2}, Boolean.TRUE},
45+
new Object[] {new Integer[] {10, 90, 49, 2, 1, 5, 23}, Boolean.FALSE}, new Object[] {new Integer[] {}, Boolean.TRUE}, new Object[] {new Integer[] {1}, Boolean.TRUE}, new Object[] {new Integer[] {2, 1}, Boolean.TRUE}, new Object[] {new Integer[] {4, 3, 2, 5}, Boolean.FALSE},
46+
new Object[] {new Double[] {4.0, 3.0, 5.1, 2.1, 3.3, 1.1, 2.2}, Boolean.TRUE}, new Object[] {new Double[] {10.1, 2.0, 2.0}, Boolean.TRUE}, new Object[] {new String[] {"a", "b", "c", "d"}, Boolean.FALSE}, new Object[] {new String[] {"b", "a", "b", "a", "b"}, Boolean.TRUE});
47+
}
48+
}

0 commit comments

Comments
 (0)