Repository: TheAlgorithms/Java
Pull request #5255 (2024-06-25 20:39:49)

feat: add `SelectionSortRecursive`
Add SelectionSort using recursion and corresponding Test for it.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5255?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `94.11765%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 39.88%. Comparing base [(`a710fe1`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a710fe11c47bdfb80342f851c189d167035b7b1c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`0e560a0`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/0e560a0c924b662705a96210b3a994cba9f21ffa?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 3 commits behind head on master.

> :exclamation: **Current head 0e560a0 differs from pull request most recent head 077c6d4**
> 
> Please [upload](https://docs.codecov.com/docs/codecov-uploader) reports for the commit 077c6d4 to get more accurate results.

| [Files](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5255?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...om/thealgorithms/sorts/SelectionSortRecursive.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5255?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fsorts%2FSelectionSortRecursive.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zb3J0cy9TZWxlY3Rpb25Tb3J0UmVjdXJzaXZlLmphdmE=) | 94.11% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5255?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5255      +/-   ##
============================================
+ Coverage     39.82%   39.88%   +0.05%     
- Complexity     2437     2445       +8     
============================================
  Files           517      518       +1     
  Lines         15476    15488      +12     
  Branches       2958     2961       +3     
============================================
+ Hits           6164     6178      +14     
+ Misses         9022     9018       -4     
- Partials        290      292       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5255?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

alxkm: Recursive method findMinIndex replaced with iterative. To fix StackOverFlowError on huge arrays for two test cases.

Patch:
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 <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;
+    }
+}


Test patch:
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();
+    }
+}