Skip to content

refactor: SelectionSort like classes and their tests #5265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 18 additions & 33 deletions src/main/java/com/thealgorithms/sorts/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
package com.thealgorithms.sorts;

public class SelectionSort implements SortAlgorithm {

/**
* Generic selection sort algorithm in increasing order.
* Sorts an array of comparable elements in increasing order using the selection sort algorithm.
*
* @param arr the array to be sorted.
* @param <T> the class of array.
* @return sorted array.
* @param array the array to be sorted
* @param <T> the class of array elements
* @return the sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[minIndex].compareTo(arr[j]) > 0) {
minIndex = j;
}
}
if (minIndex != i) {
SortUtils.swap(arr, i, minIndex);
}
}
return arr;
}
public <T extends Comparable<T>> T[] sort(T[] array) {
// One by one move the boundary of the unsorted subarray
for (int i = 0; i < array.length - 1; i++) {

/**
* Driver Code
*/
public static void main(String[] args) {
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
SelectionSort selectionSort = new SelectionSort();
Integer[] sorted = selectionSort.sort(arr);
for (int i = 0; i < sorted.length - 1; ++i) {
assert sorted[i] <= sorted[i + 1];
// Swap the remaining minimum element with the current element
SortUtils.swap(array, i, findIndexOfMin(array, i));
}
return array;
}

String[] strings = {"c", "a", "e", "b", "d"};
String[] sortedStrings = selectionSort.sort(strings);
for (int i = 0; i < sortedStrings.length - 1; ++i) {
assert strings[i].compareTo(strings[i + 1]) <= 0;
private static <T extends Comparable<T>> int findIndexOfMin(T[] array, final int start) {
int minIndex = start;
for (int i = start + 1; i < array.length; i++) {
if (array[i].compareTo(array[minIndex]) < 0) {
minIndex = i;
}
}
return minIndex;
}
}
31 changes: 13 additions & 18 deletions src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array,
return;
}

// Find the minimum element in the remaining unsorted array
final int minIndex = findMinIndex(array, index);

// Swap the found minimum element with the element at the current index
if (minIndex != index) {
SortUtils.swap(array, index, minIndex);
}
SortUtils.swap(array, index, findMinIndex(array, index));

// Recursively call selection sort for the remaining array
recursiveSelectionSort(array, index + 1);
Expand All @@ -47,20 +41,21 @@ private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array,
/**
* Finds the index of the minimum element in the array starting from the given index.
*
* @param array the array to search in.
* @param start the starting index.
* @param <T> the type of the elements in the array, which must be Comparable.
* @return the index of the minimum element starting from the given index.
* @param array the array to search
* @param start the starting index for the search
* @param <T> the type of elements in the array
* @return the index of the minimum element
*/
private static <T extends Comparable<T>> int findMinIndex(T[] array, int start) {
int currentMinIndex = start;

for (int currentIndex = start + 1; currentIndex < array.length; currentIndex++) {
if (array[currentIndex].compareTo(array[currentMinIndex]) < 0) {
currentMinIndex = currentIndex;
}
// Base case: if start is the last index, return start
if (start == array.length - 1) {
return start;
}

return currentMinIndex;
// Recursive call to find the minimum index in the rest of the array
final int minIndexInRest = findMinIndex(array, start + 1);

// Return the index of the smaller element between array[start] and the minimum element in the rest of the array
return array[start].compareTo(array[minIndexInRest]) < 0 ? start : minIndexInRest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ public class SelectionSortRecursiveTest extends SortingAlgorithmTest {
SortAlgorithm getSortAlgorithm() {
return new SelectionSortRecursive();
}

protected int getGeneratedArraySize() {
return 5000;
}
}
35 changes: 4 additions & 31 deletions src/test/java/com/thealgorithms/sorts/SelectionSortTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
package com.thealgorithms.sorts;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.api.Test;

class SelectionSortTest {

@Test
// valid test case
void integerArrTest() {
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
SelectionSort selectionSort = new SelectionSort();

assertArrayEquals(new Integer[] {1, 4, 6, 9, 12, 23, 54, 78, 231}, selectionSort.sort(arr));
}

@Test
// valid test case
void stringArrTest() {
String[] arr = {"c", "a", "e", "b", "d"};
SelectionSort selectionSort = new SelectionSort();

assertArrayEquals(new String[] {"a", "b", "c", "d", "e"}, selectionSort.sort(arr));
}

@Test
// invalid test case
void emptyArrTest() {
Integer[] arr = {};
SelectionSort selectionSort = new SelectionSort();

assertArrayEquals(new Integer[] {}, selectionSort.sort(arr));
class SelectionSortTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new SelectionSort();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
public abstract class SortingAlgorithmTest {
abstract SortAlgorithm getSortAlgorithm();

protected int getGeneratedArraySize() {
return 10_000;
}

@Test
void shouldAcceptWhenEmptyArrayIsPassed() {
Integer[] array = new Integer[] {};
Expand Down Expand Up @@ -153,15 +157,15 @@ void shouldAcceptWhenStringValueListIsPassed() {

@Test
void shouldAcceptWhenRandomArrayIsPassed() {
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
int randomSize = SortUtilsRandomGenerator.generateInt(getGeneratedArraySize());
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
Double[] sorted = getSortAlgorithm().sort(array);
assertTrue(SortUtils.isSorted(sorted));
}

@Test
void shouldAcceptWhenRandomListIsPassed() {
int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
int randomSize = SortUtilsRandomGenerator.generateInt(getGeneratedArraySize());
Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
List<Double> list = List.of(array);
List<Double> sorted = getSortAlgorithm().sort(list);
Expand Down