Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cbb2271

Browse files
author
Alex Klymenko
committedJun 28, 2024·
Refactor: Fix failed build, when generated test case for recursion is too big. To avoid stackoverflow
1 parent 6fe684c commit cbb2271

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed
 

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,21 @@ private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array,
4747
/**
4848
* Finds the index of the minimum element in the array starting from the given index.
4949
*
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.
50+
* @param array the array to search
51+
* @param start the starting index for the search
52+
* @param <T> the type of elements in the array
53+
* @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+
final 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
}

‎src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ public class SelectionSortRecursiveTest extends SortingAlgorithmTest {
55
SortAlgorithm getSortAlgorithm() {
66
return new SelectionSortRecursive();
77
}
8+
9+
protected int getGeneratedArraySize() {
10+
return 5000;
11+
}
812
}

‎src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
public abstract class SortingAlgorithmTest {
1212
abstract SortAlgorithm getSortAlgorithm();
1313

14+
protected int getGeneratedArraySize() {
15+
return 10_000;
16+
}
17+
1418
@Test
1519
void shouldAcceptWhenEmptyArrayIsPassed() {
1620
Integer[] array = new Integer[] {};
@@ -153,15 +157,15 @@ void shouldAcceptWhenStringValueListIsPassed() {
153157

154158
@Test
155159
void shouldAcceptWhenRandomArrayIsPassed() {
156-
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
160+
int randomSize = SortUtilsRandomGenerator.generateInt(getGeneratedArraySize());
157161
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
158162
Double[] sorted = getSortAlgorithm().sort(array);
159163
assertTrue(SortUtils.isSorted(sorted));
160164
}
161165

162166
@Test
163167
void shouldAcceptWhenRandomListIsPassed() {
164-
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
168+
int randomSize = SortUtilsRandomGenerator.generateInt(getGeneratedArraySize());
165169
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
166170
List<Double> list = List.of(array);
167171
List<Double> sorted = getSortAlgorithm().sort(list);

0 commit comments

Comments
 (0)
Please sign in to comment.