diff --git a/DIRECTORY.md b/DIRECTORY.md index c4eb2a6d..c033fe30 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -515,6 +515,7 @@ * [QuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/QuickSort.java) * [RadixSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/RadixSort.java) * [SelectionSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SelectionSort.java) + * [SelectionSortRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java) * [ShellSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/ShellSort.java) * [SimpleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SimpleSort.java) * [SlowSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SlowSort.java) @@ -875,6 +876,7 @@ * [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java) * [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java) * [SelectionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java) + * [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java) * [ShellSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/ShellSortTest.java) * [SimpleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SimpleSortTest.java) * [SlowSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SlowSortTest.java) diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java new file mode 100644 index 00000000..1326bb5e --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java @@ -0,0 +1,66 @@ +package com.thealgorithms.sorts; + +/** + * Class that implements the Selection Sort algorithm using recursion. + */ +public class SelectionSortRecursive implements SortAlgorithm { + + /** + * Sorts an array using recursive selection sort. + * + * @param array the array to be sorted + * @param the type of elements in the array (must be Comparable) + * @return the sorted array + */ + public > T[] sort(T[] array) { + if (array.length == 0) { + return array; + } + recursiveSelectionSort(array, 0); + return array; + } + + /** + * Recursively sorts the array using selection sort. + * + * @param array the array to be sorted + * @param index the current index to start sorting from + * @param the type of elements in the array (must be Comparable) + */ + private static > void recursiveSelectionSort(T[] array, int index) { + if (index == array.length - 1) { + return; + } + + // Find the minimum element in the remaining unsorted array + final int minIndex = findMinIndex(array, index); + + // Swap the found minimum element with the element at the current index + if (minIndex != index) { + SortUtils.swap(array, index, minIndex); + } + + // Recursively call selection sort for the remaining array + recursiveSelectionSort(array, index + 1); + } + + /** + * Finds the index of the minimum element in the array starting from the given index. + * + * @param array the array to search in. + * @param start the starting index. + * @param the type of the elements in the array, which must be Comparable. + * @return the index of the minimum element starting from the given index. + */ + private static > int findMinIndex(T[] array, int start) { + int currentMinIndex = start; + + for (int currentIndex = start + 1; currentIndex < array.length; currentIndex++) { + if (array[currentIndex].compareTo(array[currentMinIndex]) < 0) { + currentMinIndex = currentIndex; + } + } + + return currentMinIndex; + } +} diff --git a/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java b/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java new file mode 100644 index 00000000..48025f4c --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java @@ -0,0 +1,8 @@ +package com.thealgorithms.sorts; + +public class SelectionSortRecursiveTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new SelectionSortRecursive(); + } +}