feat: add WaveSort
(#5252)
* Implementing WaveSort Algorithm * Refactor: Tests to ParameterizedTest * Checkstyle: Fix wrong align * Checkstyle: Fix wrong align for second line * Checkstyle: Remove redundant line * Naming: fix method name * Documentation: adding links * Fix: adding test for isWaveSorted method * Documentation: adding description for WaveSort * Testing: test wave sort assert * Checkstyle: remove redundant whitespace * Checkstyle: remove redundant newline * Testing: improving tests --------- Co-authored-by: alxklm <alx@alx.com> Co-authored-by: vil02 <65706193+vil02@users.noreply.github.com>
This commit is contained in:
parent
971f5fc85b
commit
7054535d36
@ -528,6 +528,7 @@
|
||||
* [TopologicalSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/TopologicalSort.java)
|
||||
* [TreeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/TreeSort.java)
|
||||
* [WiggleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/WiggleSort.java)
|
||||
* [WaveSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/WaveSort.java)
|
||||
* stacks
|
||||
* [BalancedBrackets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java)
|
||||
* [DecimalToAnyUsingStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java)
|
||||
@ -885,6 +886,7 @@
|
||||
* [TopologicalSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/TopologicalSortTest.java)
|
||||
* [TreeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/TreeSortTest.java)
|
||||
* [WiggleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/WiggleSortTest.java)
|
||||
* [WaveSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/WaveSortTest.java)
|
||||
* stacks
|
||||
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)
|
||||
* strings
|
||||
|
46
src/main/java/com/thealgorithms/sorts/WaveSort.java
Normal file
46
src/main/java/com/thealgorithms/sorts/WaveSort.java
Normal file
@ -0,0 +1,46 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
/**
|
||||
* The WaveSort algorithm sorts an array so that every alternate element is greater than its adjacent elements.
|
||||
* This implementation also provides a method to check if an array is wave sorted.
|
||||
*/
|
||||
public class WaveSort implements SortAlgorithm {
|
||||
/**
|
||||
* Sorts the given array such that every alternate element is greater than its adjacent elements.
|
||||
*
|
||||
* @param array The array to be sorted.
|
||||
* @param <T> The type of elements in the array, which must be Comparable.
|
||||
* @return The sorted array.
|
||||
*/
|
||||
@Override
|
||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
||||
for (int i = 0; i < array.length; i += 2) {
|
||||
if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
|
||||
SortUtils.swap(array, i, i - 1);
|
||||
}
|
||||
if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
|
||||
SortUtils.swap(array, i, i + 1);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given array is wave sorted. An array is wave sorted if every alternate element is greater than its adjacent elements.
|
||||
*
|
||||
* @param array The array to check.
|
||||
* @param <T> The type of elements in the array, which must be Comparable.
|
||||
* @return true if the array is wave sorted, false otherwise.
|
||||
*/
|
||||
public <T extends Comparable<T>> boolean isWaveSorted(T[] array) {
|
||||
for (int i = 0; i < array.length; i += 2) {
|
||||
if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
|
||||
return false;
|
||||
}
|
||||
if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
48
src/test/java/com/thealgorithms/sorts/WaveSortTest.java
Normal file
48
src/test/java/com/thealgorithms/sorts/WaveSortTest.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
public class WaveSortTest {
|
||||
@ParameterizedTest
|
||||
@MethodSource("arraysToWaveSort")
|
||||
public void waveSortTest(Integer[] array) {
|
||||
WaveSort waveSort = new WaveSort();
|
||||
final var inputHistogram = getHistogram(array);
|
||||
final var sortedArray = waveSort.sort(array);
|
||||
assertTrue(waveSort.isWaveSorted(sortedArray));
|
||||
final var sortedHistogram = getHistogram(sortedArray);
|
||||
assertEquals(inputHistogram, sortedHistogram, "Element counts do not match");
|
||||
}
|
||||
|
||||
private Map<Integer, Integer> getHistogram(Integer[] array) {
|
||||
Map<Integer, Integer> histogram = new HashMap<>();
|
||||
for (final var element : array) {
|
||||
histogram.put(element, histogram.getOrDefault(element, 0) + 1);
|
||||
}
|
||||
return histogram;
|
||||
}
|
||||
|
||||
private static Stream<Object[]> arraysToWaveSort() {
|
||||
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}},
|
||||
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}});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("waveSortedArrays")
|
||||
public <T extends Comparable<T>> void testIsWaveSorted(T[] array, boolean expected) {
|
||||
final WaveSort waveSort = new WaveSort();
|
||||
assertEquals(expected, waveSort.isWaveSorted(array));
|
||||
}
|
||||
public static Stream<Object[]> waveSortedArrays() {
|
||||
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},
|
||||
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},
|
||||
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});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user