Skip to content

Commit 0087444

Browse files
alxkmalxklmvil02
authored
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 <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent 224ee3d commit 0087444

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@
515515
* [QuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/QuickSort.java)
516516
* [RadixSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/RadixSort.java)
517517
* [SelectionSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SelectionSort.java)
518+
* [SelectionSortRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java)
518519
* [ShellSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/ShellSort.java)
519520
* [SimpleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SimpleSort.java)
520521
* [SlowSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/SlowSort.java)
@@ -875,6 +876,7 @@
875876
* [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java)
876877
* [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java)
877878
* [SelectionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java)
879+
* [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java)
878880
* [ShellSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/ShellSortTest.java)
879881
* [SimpleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SimpleSortTest.java)
880882
* [SlowSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SlowSortTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithms.sorts;
2+
3+
/**
4+
* Class that implements the Selection Sort algorithm using recursion.
5+
*/
6+
public class SelectionSortRecursive implements SortAlgorithm {
7+
8+
/**
9+
* Sorts an array using recursive selection sort.
10+
*
11+
* @param array the array to be sorted
12+
* @param <T> the type of elements in the array (must be Comparable)
13+
* @return the sorted array
14+
*/
15+
public <T extends Comparable<T>> T[] sort(T[] array) {
16+
if (array.length == 0) {
17+
return array;
18+
}
19+
recursiveSelectionSort(array, 0);
20+
return array;
21+
}
22+
23+
/**
24+
* Recursively sorts the array using selection sort.
25+
*
26+
* @param array the array to be sorted
27+
* @param index the current index to start sorting from
28+
* @param <T> the type of elements in the array (must be Comparable)
29+
*/
30+
private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array, int index) {
31+
if (index == array.length - 1) {
32+
return;
33+
}
34+
35+
// Find the minimum element in the remaining unsorted array
36+
final int minIndex = findMinIndex(array, index);
37+
38+
// Swap the found minimum element with the element at the current index
39+
if (minIndex != index) {
40+
SortUtils.swap(array, index, minIndex);
41+
}
42+
43+
// Recursively call selection sort for the remaining array
44+
recursiveSelectionSort(array, index + 1);
45+
}
46+
47+
/**
48+
* Finds the index of the minimum element in the array starting from the given index.
49+
*
50+
* @param array the array to search in.
51+
* @param start the starting index.
52+
* @param <T> the type of the elements in the array, which must be Comparable.
53+
* @return the index of the minimum element starting from the given index.
54+
*/
55+
private static <T extends Comparable<T>> int findMinIndex(T[] array, int start) {
56+
int currentMinIndex = start;
57+
58+
for (int currentIndex = start + 1; currentIndex < array.length; currentIndex++) {
59+
if (array[currentIndex].compareTo(array[currentMinIndex]) < 0) {
60+
currentMinIndex = currentIndex;
61+
}
62+
}
63+
64+
return currentMinIndex;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class SelectionSortRecursiveTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new SelectionSortRecursive();
7+
}
8+
}

0 commit comments

Comments
 (0)