From 41f76e0e89478c9c806b8bcbfbd72867b267ba91 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 13 Aug 2024 18:26:48 +0200 Subject: [PATCH] refactor: simple improvements and cleanup for different sorts (#5320) --- .../com/thealgorithms/sorts/CircleSort.java | 28 ++++---- .../com/thealgorithms/sorts/OddEvenSort.java | 40 ++++++----- .../thealgorithms/sorts/SelectionSort.java | 13 ++-- .../com/thealgorithms/sorts/SimpleSort.java | 40 +---------- .../com/thealgorithms/sorts/StrandSort.java | 4 +- .../thealgorithms/sorts/SimpleSortTest.java | 66 ++----------------- 6 files changed, 55 insertions(+), 136 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/CircleSort.java b/src/main/java/com/thealgorithms/sorts/CircleSort.java index 25c308b1..b9b41be1 100644 --- a/src/main/java/com/thealgorithms/sorts/CircleSort.java +++ b/src/main/java/com/thealgorithms/sorts/CircleSort.java @@ -7,25 +7,29 @@ public class CircleSort implements SortAlgorithm { */ @Override public > T[] sort(T[] array) { - int n = array.length; - if (n == 0) { + if (array.length == 0) { return array; } - while (doSort(array, 0, n - 1)) { + while (doSort(array, 0, array.length - 1)) { } return array; } - /* This method implements the cyclic sort recursive version + /** + * Recursively sorts the array in a circular manner by comparing elements + * from the start and end of the current segment. + * + * @param The type of elements in the array, which must be comparable * @param array The array to be sorted - * @param the left boundary of the part currently being sorted - * @param the right boundary of the part currently being sorted + * @param left The left boundary of the current segment being sorted + * @param right The right boundary of the current segment being sorted + * @return true if any elements were swapped during the sort; false otherwise */ - private > Boolean doSort(T[] array, int left, int right) { + private > boolean doSort(final T[] array, final int left, final int right) { boolean swapped = false; if (left == right) { - return Boolean.FALSE; + return false; } int low = left; @@ -45,10 +49,10 @@ public class CircleSort implements SortAlgorithm { swapped = true; } - int mid = left + (right - left) / 2; - Boolean leftHalf = doSort(array, left, mid); - Boolean rightHalf = doSort(array, mid + 1, right); + final int mid = left + (right - left) / 2; + final boolean leftHalfSwapped = doSort(array, left, mid); + final boolean rightHalfSwapped = doSort(array, mid + 1, right); - return swapped || leftHalf || rightHalf; + return swapped || leftHalfSwapped || rightHalfSwapped; } } diff --git a/src/main/java/com/thealgorithms/sorts/OddEvenSort.java b/src/main/java/com/thealgorithms/sorts/OddEvenSort.java index 8db85f39..ac94982c 100644 --- a/src/main/java/com/thealgorithms/sorts/OddEvenSort.java +++ b/src/main/java/com/thealgorithms/sorts/OddEvenSort.java @@ -13,29 +13,39 @@ public final class OddEvenSort implements SortAlgorithm { * Sorts the given array using the Odd-Even Sort algorithm. * * @param the type of elements in the array, which must implement the Comparable interface - * @param arr the array to be sorted + * @param array the array to be sorted * @return the sorted array */ @Override - public > T[] sort(T[] arr) { + public > T[] sort(T[] array) { boolean sorted = false; while (!sorted) { - sorted = true; + sorted = performOddSort(array); + sorted = performEvenSort(array) && sorted; + } - for (int i = 1; i < arr.length - 1; i += 2) { - if (arr[i].compareTo(arr[i + 1]) > 0) { - SortUtils.swap(arr, i, i + 1); - sorted = false; - } - } + return array; + } - for (int i = 0; i < arr.length - 1; i += 2) { - if (arr[i].compareTo(arr[i + 1]) > 0) { - SortUtils.swap(arr, i, i + 1); - sorted = false; - } + private > boolean performOddSort(T[] array) { + boolean sorted = true; + for (int i = 1; i < array.length - 1; i += 2) { + if (array[i].compareTo(array[i + 1]) > 0) { + SortUtils.swap(array, i, i + 1); + sorted = false; } } - return arr; + return sorted; + } + + private > boolean performEvenSort(T[] array) { + boolean sorted = true; + for (int i = 0; i < array.length - 1; i += 2) { + if (array[i].compareTo(array[i + 1]) > 0) { + SortUtils.swap(array, i, i + 1); + sorted = false; + } + } + return sorted; } } diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSort.java b/src/main/java/com/thealgorithms/sorts/SelectionSort.java index 8c815c6c..dbb2b88f 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSort.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSort.java @@ -10,18 +10,17 @@ public class SelectionSort implements SortAlgorithm { */ @Override public > T[] sort(T[] array) { - // One by one move the boundary of the unsorted subarray - for (int i = 0; i < array.length - 1; i++) { - // Swap the remaining minimum element with the current element - SortUtils.swap(array, i, findIndexOfMin(array, i)); + for (int i = 0; i < array.length - 1; i++) { + final int minIndex = findIndexOfMin(array, i); + SortUtils.swap(array, i, minIndex); } return array; } - private static > int findIndexOfMin(T[] array, final int start) { - int minIndex = start; - for (int i = start + 1; i < array.length; i++) { + private static > int findIndexOfMin(T[] array, final int startIndex) { + int minIndex = startIndex; + for (int i = startIndex + 1; i < array.length; i++) { if (array[i].compareTo(array[minIndex]) < 0) { minIndex = i; } diff --git a/src/main/java/com/thealgorithms/sorts/SimpleSort.java b/src/main/java/com/thealgorithms/sorts/SimpleSort.java index 110bc6e4..a03223cb 100644 --- a/src/main/java/com/thealgorithms/sorts/SimpleSort.java +++ b/src/main/java/com/thealgorithms/sorts/SimpleSort.java @@ -1,51 +1,15 @@ package com.thealgorithms.sorts; public class SimpleSort implements SortAlgorithm { - @Override public > T[] sort(T[] array) { - final int length = array.length; - - for (int i = 0; i < length; i++) { - for (int j = i + 1; j < length; j++) { + for (int i = 0; i < array.length; i++) { + for (int j = i + 1; j < array.length; j++) { if (SortUtils.less(array[j], array[i])) { SortUtils.swap(array, i, j); } } } - return array; } - - public static void main(String[] args) { - // ==== Int ======= - Integer[] a = {3, 7, 45, 1, 33, 5, 2, 9}; - System.out.print("unsorted: "); - SortUtils.print(a); - System.out.println(); - - new SimpleSort().sort(a); - System.out.print("sorted: "); - SortUtils.print(a); - System.out.println(); - - // ==== String ======= - String[] b = { - "banana", - "berry", - "orange", - "grape", - "peach", - "cherry", - "apple", - "pineapple", - }; - System.out.print("unsorted: "); - SortUtils.print(b); - System.out.println(); - - new SimpleSort().sort(b); - System.out.print("sorted: "); - SortUtils.print(b); - } } diff --git a/src/main/java/com/thealgorithms/sorts/StrandSort.java b/src/main/java/com/thealgorithms/sorts/StrandSort.java index 45bbe88d..147d1134 100644 --- a/src/main/java/com/thealgorithms/sorts/StrandSort.java +++ b/src/main/java/com/thealgorithms/sorts/StrandSort.java @@ -38,9 +38,9 @@ public final class StrandSort implements SortAlgorithm { List result = new ArrayList<>(); while (!list.isEmpty()) { final List sorted = new ArrayList<>(); - sorted.add(list.remove(0)); + sorted.add(list.removeFirst()); for (int i = 0; i < list.size();) { - if (sorted.get(sorted.size() - 1).compareTo(list.get(i)) <= 0) { + if (sorted.getLast().compareTo(list.get(i)) <= 0) { sorted.add(list.remove(i)); } else { i++; diff --git a/src/test/java/com/thealgorithms/sorts/SimpleSortTest.java b/src/test/java/com/thealgorithms/sorts/SimpleSortTest.java index e476b97b..887b314e 100644 --- a/src/test/java/com/thealgorithms/sorts/SimpleSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/SimpleSortTest.java @@ -1,66 +1,8 @@ package com.thealgorithms.sorts; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - -import org.junit.jupiter.api.Test; - -public class SimpleSortTest { - - private SimpleSort simpleSort = new SimpleSort(); - - @Test - public void simpleSortEmptyArray() { - Integer[] inputArray = {}; - Integer[] outputArray = simpleSort.sort(inputArray); - Integer[] expectedOutput = {}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void simpleSortSingleIntegerArray() { - Integer[] inputArray = {4}; - Integer[] outputArray = simpleSort.sort(inputArray); - Integer[] expectedOutput = {4}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void simpleSortSingleStringArray() { - String[] inputArray = {"s"}; - String[] outputArray = simpleSort.sort(inputArray); - String[] expectedOutput = {"s"}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void simpleSortNonDuplicateIntegerArray() { - Integer[] inputArray = {6, -1, 99, 27, -15, 23, -36}; - Integer[] outputArray = simpleSort.sort(inputArray); - Integer[] expectedOutput = {-36, -15, -1, 6, 23, 27, 99}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void simpleSortDuplicateIntegerArray() { - Integer[] inputArray = {6, -1, 27, -15, 23, 27, -36, 23}; - Integer[] outputArray = simpleSort.sort(inputArray); - Integer[] expectedOutput = {-36, -15, -1, 6, 23, 23, 27, 27}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void simpleSortNonDuplicateStringArray() { - String[] inputArray = {"s", "b", "k", "a", "d", "c", "h"}; - String[] outputArray = simpleSort.sort(inputArray); - String[] expectedOutput = {"a", "b", "c", "d", "h", "k", "s"}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void simpleSortDuplicateStringArray() { - String[] inputArray = {"s", "b", "d", "a", "d", "c", "h", "b"}; - String[] outputArray = simpleSort.sort(inputArray); - String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"}; - assertArrayEquals(outputArray, expectedOutput); +public class SimpleSortTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new SimpleSort(); } }