Skip to content

Commit 5c34fea

Browse files
author
Alex Klymenko
committed
Fix: change findMinIndex method to recursive
1 parent 38926cc commit 5c34fea

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array,
5353
* @return the index of the minimum element
5454
*/
5555
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-
}
56+
// Base case: if start is the last index, return start
57+
if (start == array.length - 1) {
58+
return start;
6259
}
6360

64-
return currentMinIndex;
61+
// Recursive call to find the minimum index in the rest of the array
62+
int minIndexInRest = findMinIndex(array, start + 1);
63+
64+
// Return the index of the smaller element between array[start] and the minimum element in the rest of the array
65+
return array[start].compareTo(array[minIndexInRest]) < 0 ? start : minIndexInRest;
6566
}
6667
}

0 commit comments

Comments
 (0)