feat: add SelectionSortRecursive (#5255)

* Implementation: SelectionSort using recursion

* Documentation: adding links

* Fix issue with findMinIndex

* Fix: change findMinIndex method to recursive

* Fix: improve variable change scope

* Fix: Replacing recursive method findMinIndex with iterative. To fix StackOverFlow on huge arrays

* refactor: remove `null` check

* Fix: Removing redundant null check

---------

Co-authored-by: alxklm <alx@alx.com>
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
Alex Klymenko 2024-06-28 10:18:06 +03:00 committed by GitHub
parent 224ee3d227
commit 0087444e9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 76 additions and 0 deletions

View File

@ -515,6 +515,7 @@
* [QuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/QuickSort.java) * [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) * [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) * [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) * [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) * [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) * [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) * [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) * [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) * [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) * [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) * [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) * [SlowSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SlowSortTest.java)

View File

@ -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 <T> the type of elements in the array (must be Comparable)
* @return the sorted array
*/
public <T extends Comparable<T>> 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 <T> the type of elements in the array (must be Comparable)
*/
private static <T extends Comparable<T>> 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 <T> 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 <T extends Comparable<T>> 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;
}
}

View File

@ -0,0 +1,8 @@
package com.thealgorithms.sorts;
public class SelectionSortRecursiveTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new SelectionSortRecursive();
}
}