Skip to content

Commit 20e7a3a

Browse files
alxkmAlex Klymenkovil02
authored
refactor: SelectionSort like classes and their tests (#5265)
* Refactor: Adding test common approach, adding javadocs, renaming variables * Refactor: Fix failed build, when generated test case for recursion is too big. To avoid stackoverflow * Checkstyle: Adding newline to end of class * refactor: simplify assign minIndex in recursiveSelectionSort, and improving SelectionSort * checkstyle: adding newline to SelectionSort --------- Co-authored-by: Alex Klymenko <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent 0087444 commit 20e7a3a

File tree

5 files changed

+45
-84
lines changed

5 files changed

+45
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,31 @@
11
package com.thealgorithms.sorts;
22

33
public class SelectionSort implements SortAlgorithm {
4-
54
/**
6-
* Generic selection sort algorithm in increasing order.
5+
* Sorts an array of comparable elements in increasing order using the selection sort algorithm.
76
*
8-
* @param arr the array to be sorted.
9-
* @param <T> the class of array.
10-
* @return sorted array.
7+
* @param array the array to be sorted
8+
* @param <T> the class of array elements
9+
* @return the sorted array
1110
*/
1211
@Override
13-
public <T extends Comparable<T>> T[] sort(T[] arr) {
14-
int n = arr.length;
15-
for (int i = 0; i < n - 1; i++) {
16-
int minIndex = i;
17-
for (int j = i + 1; j < n; j++) {
18-
if (arr[minIndex].compareTo(arr[j]) > 0) {
19-
minIndex = j;
20-
}
21-
}
22-
if (minIndex != i) {
23-
SortUtils.swap(arr, i, minIndex);
24-
}
25-
}
26-
return arr;
27-
}
12+
public <T extends Comparable<T>> T[] sort(T[] array) {
13+
// One by one move the boundary of the unsorted subarray
14+
for (int i = 0; i < array.length - 1; i++) {
2815

29-
/**
30-
* Driver Code
31-
*/
32-
public static void main(String[] args) {
33-
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
34-
SelectionSort selectionSort = new SelectionSort();
35-
Integer[] sorted = selectionSort.sort(arr);
36-
for (int i = 0; i < sorted.length - 1; ++i) {
37-
assert sorted[i] <= sorted[i + 1];
16+
// Swap the remaining minimum element with the current element
17+
SortUtils.swap(array, i, findIndexOfMin(array, i));
3818
}
19+
return array;
20+
}
3921

40-
String[] strings = {"c", "a", "e", "b", "d"};
41-
String[] sortedStrings = selectionSort.sort(strings);
42-
for (int i = 0; i < sortedStrings.length - 1; ++i) {
43-
assert strings[i].compareTo(strings[i + 1]) <= 0;
22+
private static <T extends Comparable<T>> int findIndexOfMin(T[] array, final int start) {
23+
int minIndex = start;
24+
for (int i = start + 1; i < array.length; i++) {
25+
if (array[i].compareTo(array[minIndex]) < 0) {
26+
minIndex = i;
27+
}
4428
}
29+
return minIndex;
4530
}
4631
}

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

+13-18
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array,
3232
return;
3333
}
3434

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-
}
35+
SortUtils.swap(array, index, findMinIndex(array, index));
4236

4337
// Recursively call selection sort for the remaining array
4438
recursiveSelectionSort(array, index + 1);
@@ -47,20 +41,21 @@ private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array,
4741
/**
4842
* Finds the index of the minimum element in the array starting from the given index.
4943
*
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.
44+
* @param array the array to search
45+
* @param start the starting index for the search
46+
* @param <T> the type of elements in the array
47+
* @return the index of the minimum element
5448
*/
5549
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-
}
50+
// Base case: if start is the last index, return start
51+
if (start == array.length - 1) {
52+
return start;
6253
}
6354

64-
return currentMinIndex;
55+
// Recursive call to find the minimum index in the rest of the array
56+
final int minIndexInRest = findMinIndex(array, start + 1);
57+
58+
// Return the index of the smaller element between array[start] and the minimum element in the rest of the array
59+
return array[start].compareTo(array[minIndexInRest]) < 0 ? start : minIndexInRest;
6560
}
6661
}

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

+4
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
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
5-
import org.junit.jupiter.api.Test;
6-
7-
class SelectionSortTest {
8-
9-
@Test
10-
// valid test case
11-
void integerArrTest() {
12-
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
13-
SelectionSort selectionSort = new SelectionSort();
14-
15-
assertArrayEquals(new Integer[] {1, 4, 6, 9, 12, 23, 54, 78, 231}, selectionSort.sort(arr));
16-
}
17-
18-
@Test
19-
// valid test case
20-
void stringArrTest() {
21-
String[] arr = {"c", "a", "e", "b", "d"};
22-
SelectionSort selectionSort = new SelectionSort();
23-
24-
assertArrayEquals(new String[] {"a", "b", "c", "d", "e"}, selectionSort.sort(arr));
25-
}
26-
27-
@Test
28-
// invalid test case
29-
void emptyArrTest() {
30-
Integer[] arr = {};
31-
SelectionSort selectionSort = new SelectionSort();
32-
33-
assertArrayEquals(new Integer[] {}, selectionSort.sort(arr));
3+
class SelectionSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new SelectionSort();
347
}
358
}

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

+6-2
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)