From 6fe684c3a851fc048967b696122b599e897af9d0 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Fri, 28 Jun 2024 13:39:10 +0200 Subject: [PATCH 1/5] Refactor: Adding test common approach, adding javadocs, renaming variables --- .../thealgorithms/sorts/SelectionSort.java | 53 ++++++++----------- .../sorts/SelectionSortTest.java | 35 ++---------- 2 files changed, 27 insertions(+), 61 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSort.java b/src/main/java/com/thealgorithms/sorts/SelectionSort.java index cc7b3cc6b662..a062623e7f1d 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSort.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSort.java @@ -1,46 +1,39 @@ package com.thealgorithms.sorts; +/** + * This class provides an implementation of the selection sort algorithm. + * It sorts an array of elements in increasing order using an iterative approach. + */ 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 the class of array. - * @return sorted array. + * @param array the array to be sorted + * @param the class of array elements + * @return the sorted array */ @Override - public > T[] sort(T[] arr) { - int n = arr.length; - for (int i = 0; i < n - 1; i++) { + public > T[] sort(T[] array) { + if (array.length == 0) { + return array; + } + + // One by one move the boundary of the unsorted subarray + for (int i = 0; i < array.length - 1; i++) { + // Find the minimum element in the unsorted array int minIndex = i; - for (int j = i + 1; j < n; j++) { - if (arr[minIndex].compareTo(arr[j]) > 0) { + for (int j = i + 1; j < array.length; j++) { + if (array[j].compareTo(array[minIndex]) < 0) { minIndex = j; } } + + // Swap the found minimum element with the first element if (minIndex != i) { - SortUtils.swap(arr, i, minIndex); + SortUtils.swap(array, i, minIndex); } } - return arr; - } - - /** - * 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]; - } - - 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; - } + return array; } -} +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java b/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java index 83fbd1ece909..fab4be1ad01b 100644 --- a/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java @@ -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(); } } From cbb2271bc1785c3949fd02c873c48e36e61c9c68 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Fri, 28 Jun 2024 13:52:12 +0200 Subject: [PATCH 2/5] Refactor: Fix failed build, when generated test case for recursion is too big. To avoid stackoverflow --- .../sorts/SelectionSortRecursive.java | 23 ++++++++++--------- .../sorts/SelectionSortRecursiveTest.java | 4 ++++ .../sorts/SortingAlgorithmTest.java | 8 +++++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java index 1326bb5e73da..c96abc232421 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java @@ -47,20 +47,21 @@ private static > 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 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 the type of elements in the array + * @return the index of the minimum element */ private static > 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; } } diff --git a/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java b/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java index 48025f4ce4f4..20dcf249b31a 100644 --- a/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java +++ b/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java @@ -5,4 +5,8 @@ public class SelectionSortRecursiveTest extends SortingAlgorithmTest { SortAlgorithm getSortAlgorithm() { return new SelectionSortRecursive(); } + + protected int getGeneratedArraySize() { + return 5000; + } } diff --git a/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java b/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java index 113bff2b5b0d..e6aedc3f06ac 100644 --- a/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java @@ -11,6 +11,10 @@ public abstract class SortingAlgorithmTest { abstract SortAlgorithm getSortAlgorithm(); + protected int getGeneratedArraySize() { + return 10_000; + } + @Test void shouldAcceptWhenEmptyArrayIsPassed() { Integer[] array = new Integer[] {}; @@ -153,7 +157,7 @@ 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)); @@ -161,7 +165,7 @@ void shouldAcceptWhenRandomArrayIsPassed() { @Test void shouldAcceptWhenRandomListIsPassed() { - int randomSize = SortUtilsRandomGenerator.generateInt(10_000); + int randomSize = SortUtilsRandomGenerator.generateInt(getGeneratedArraySize()); Double[] array = SortUtilsRandomGenerator.generateArray(randomSize); List list = List.of(array); List sorted = getSortAlgorithm().sort(list); From 10314dc046763922c4f966f8f6ba213127dccd25 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Fri, 28 Jun 2024 13:53:48 +0200 Subject: [PATCH 3/5] Checkstyle: Adding newline to end of class --- src/main/java/com/thealgorithms/sorts/SelectionSort.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSort.java b/src/main/java/com/thealgorithms/sorts/SelectionSort.java index a062623e7f1d..bb9649814a52 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSort.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSort.java @@ -5,7 +5,6 @@ * It sorts an array of elements in increasing order using an iterative approach. */ public class SelectionSort implements SortAlgorithm { - /** * Sorts an array of comparable elements in increasing order using the selection sort algorithm. * @@ -36,4 +35,4 @@ public > T[] sort(T[] array) { } return array; } -} \ No newline at end of file +} From f98d1265ad65c3bcedbf795fb90e2917738a91ed Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sat, 29 Jun 2024 09:51:18 +0200 Subject: [PATCH 4/5] refactor: simplify assign minIndex in recursiveSelectionSort, and improving SelectionSort --- .../thealgorithms/sorts/SelectionSort.java | 33 ++++++++----------- .../sorts/SelectionSortRecursive.java | 8 +---- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSort.java b/src/main/java/com/thealgorithms/sorts/SelectionSort.java index bb9649814a52..30b221b15c42 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSort.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSort.java @@ -1,9 +1,5 @@ package com.thealgorithms.sorts; -/** - * This class provides an implementation of the selection sort algorithm. - * It sorts an array of elements in increasing order using an iterative approach. - */ public class SelectionSort implements SortAlgorithm { /** * Sorts an array of comparable elements in increasing order using the selection sort algorithm. @@ -14,25 +10,22 @@ public class SelectionSort implements SortAlgorithm { */ @Override public > T[] sort(T[] array) { - if (array.length == 0) { - return array; - } - // One by one move the boundary of the unsorted subarray for (int i = 0; i < array.length - 1; i++) { - // Find the minimum element in the unsorted array - int minIndex = i; - for (int j = i + 1; j < array.length; j++) { - if (array[j].compareTo(array[minIndex]) < 0) { - minIndex = j; - } - } - // Swap the found minimum element with the first element - if (minIndex != i) { - SortUtils.swap(array, i, minIndex); - } + // Swap the remaining minimum element with the current element + SortUtils.swap(array, i, findIndexOfMin(array, i)); } return array; } -} + + private static > 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; + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java index c96abc232421..32bd58a1361a 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java @@ -32,13 +32,7 @@ private static > 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); From f44339e13cb0b7b0aff33c54570553e358a83a06 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sat, 29 Jun 2024 09:54:40 +0200 Subject: [PATCH 5/5] checkstyle: adding newline to SelectionSort --- src/main/java/com/thealgorithms/sorts/SelectionSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSort.java b/src/main/java/com/thealgorithms/sorts/SelectionSort.java index 30b221b15c42..8c815c6cb5fc 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSort.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSort.java @@ -28,4 +28,4 @@ private static > int findIndexOfMin(T[] array, final int } return minIndex; } -} \ No newline at end of file +}