Skip to content

Commit 6fe684c

Browse files
author
Alex Klymenko
committed
Refactor: Adding test common approach, adding javadocs, renaming variables
1 parent 0087444 commit 6fe684c

File tree

2 files changed

+27
-61
lines changed

2 files changed

+27
-61
lines changed
Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
11
package com.thealgorithms.sorts;
22

3+
/**
4+
* This class provides an implementation of the selection sort algorithm.
5+
* It sorts an array of elements in increasing order using an iterative approach.
6+
*/
37
public class SelectionSort implements SortAlgorithm {
48

59
/**
6-
* Generic selection sort algorithm in increasing order.
10+
* Sorts an array of comparable elements in increasing order using the selection sort algorithm.
711
*
8-
* @param arr the array to be sorted.
9-
* @param <T> the class of array.
10-
* @return sorted array.
12+
* @param array the array to be sorted
13+
* @param <T> the class of array elements
14+
* @return the sorted array
1115
*/
1216
@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++) {
17+
public <T extends Comparable<T>> T[] sort(T[] array) {
18+
if (array.length == 0) {
19+
return array;
20+
}
21+
22+
// One by one move the boundary of the unsorted subarray
23+
for (int i = 0; i < array.length - 1; i++) {
24+
// Find the minimum element in the unsorted array
1625
int minIndex = i;
17-
for (int j = i + 1; j < n; j++) {
18-
if (arr[minIndex].compareTo(arr[j]) > 0) {
26+
for (int j = i + 1; j < array.length; j++) {
27+
if (array[j].compareTo(array[minIndex]) < 0) {
1928
minIndex = j;
2029
}
2130
}
31+
32+
// Swap the found minimum element with the first element
2233
if (minIndex != i) {
23-
SortUtils.swap(arr, i, minIndex);
34+
SortUtils.swap(array, i, minIndex);
2435
}
2536
}
26-
return arr;
27-
}
28-
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];
38-
}
39-
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;
44-
}
37+
return array;
4538
}
46-
}
39+
}
Lines changed: 4 additions & 31 deletions
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
}

0 commit comments

Comments
 (0)