From 66bfaff807eb184d3f89613c90a9b5b96a89767a Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 11 Aug 2024 19:55:11 +0200 Subject: [PATCH] refactor: cleanup `CocktailShakerSort` (#5317) --- .../sorts/CocktailShakerSort.java | 98 ++++++++++++------- .../sorts/CocktailShakerSortTest.java | 68 +------------ 2 files changed, 65 insertions(+), 101 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/CocktailShakerSort.java b/src/main/java/com/thealgorithms/sorts/CocktailShakerSort.java index c88c7bd09..600ae8d3e 100644 --- a/src/main/java/com/thealgorithms/sorts/CocktailShakerSort.java +++ b/src/main/java/com/thealgorithms/sorts/CocktailShakerSort.java @@ -1,57 +1,81 @@ package com.thealgorithms.sorts; /** + * CocktailShakerSort class implements the Cocktail Shaker Sort algorithm, + * which is a bidirectional bubble sort. It sorts the array by passing + * through it back and forth, progressively moving the largest elements + * to the end and the smallest elements to the beginning. + * * @author Mateus Bizzo (https://github.com/MattBizzo) * @author Podshivalov Nikita (https://github.com/nikitap492) */ class CocktailShakerSort implements SortAlgorithm { /** - * This method implements the Generic Cocktail Shaker Sort + * Sorts the given array using the Cocktail Shaker Sort algorithm. * - * @param array The array to be sorted Sorts the array in increasing order + * @param The type of elements in the array, which must be comparable + * @param array The array to be sorted + * @return The sorted array */ @Override - public > T[] sort(T[] array) { - int length = array.length; - int left = 0; - int right = length - 1; - int swappedLeft; - int swappedRight; - while (left < right) { - // front - swappedRight = 0; - for (int i = left; i < right; i++) { - if (SortUtils.less(array[i + 1], array[i])) { - SortUtils.swap(array, i, i + 1); - swappedRight = i; - } - } - // back - right = swappedRight; - swappedLeft = length - 1; - for (int j = right; j > left; j--) { - if (SortUtils.less(array[j], array[j - 1])) { - SortUtils.swap(array, j - 1, j); - swappedLeft = j; - } - } - left = swappedLeft; + public > T[] sort(final T[] array) { + if (array.length == 0) { + return array; } + + int left = 0; + int right = array.length - 1; + + while (left < right) { + right = performForwardPass(array, left, right); + left = performBackwardPass(array, left, right); + } + return array; } - // Driver Program - public static void main(String[] args) { - // Integer Input - Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; - CocktailShakerSort shakerSort = new CocktailShakerSort(); + /** + * Performs a forward pass through the array, moving larger elements to the end. + * + * @param The type of elements in the array, which must be comparable + * @param array The array being sorted + * @param left The current left boundary of the sorting area + * @param right The current right boundary of the sorting area + * @return The index of the last swapped element during this pass + */ + private > int performForwardPass(final T[] array, final int left, final int right) { + int lastSwappedIndex = left; - // Output => 1 4 6 9 12 23 54 78 231 - SortUtils.print(shakerSort.sort(integers)); + for (int i = left; i < right; i++) { + if (SortUtils.less(array[i + 1], array[i])) { + SortUtils.swap(array, i, i + 1); + lastSwappedIndex = i; + } + } - // String Input - String[] strings = {"c", "a", "e", "b", "d"}; - SortUtils.print(shakerSort.sort(strings)); + return lastSwappedIndex; + } + + /** + * Performs a backward pass through the array, moving smaller elements to the beginning. + * + * @param The type of elements in the array, which must be comparable + * @param array The array being sorted + * @param left The current left boundary of the sorting area + * @param right The current right boundary of the sorting area + * @return The index of the last swapped element during this pass + */ + private > int performBackwardPass(final T[] array, final int left, final int right) { + int lastSwappedIndex = right; + + for (int i = right; i > left; i--) { + if (SortUtils.less(array[i], array[i - 1])) { + SortUtils.swap(array, i - 1, i); + lastSwappedIndex = i; + } + } + + return lastSwappedIndex; } } diff --git a/src/test/java/com/thealgorithms/sorts/CocktailShakerSortTest.java b/src/test/java/com/thealgorithms/sorts/CocktailShakerSortTest.java index 56628b78d..45c122027 100644 --- a/src/test/java/com/thealgorithms/sorts/CocktailShakerSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/CocktailShakerSortTest.java @@ -1,68 +1,8 @@ package com.thealgorithms.sorts; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - -import org.junit.jupiter.api.Test; - -/** - * @author Tabbygray (https://github.com/Tabbygray) - * @see CocktailShakerSort - */ -public class CocktailShakerSortTest { - - private CocktailShakerSort cocktailShakerSort = new CocktailShakerSort(); - - @Test - public void cocktailShakerSortEmptyArray() { - Integer[] inputArray = {}; - Integer[] outputArray = cocktailShakerSort.sort(inputArray); - Integer[] expectedOutput = {}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void cocktailShakerSortSingleStringElementArray() { - String[] inputArray = {"Test"}; - String[] outputArray = cocktailShakerSort.sort(inputArray); - String[] expectedOutput = {"Test"}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void cocktailShakerSortIntegerArray() { - Integer[] inputArray = {2, 92, 1, 33, -33, 27, 5, 100, 78, 99, -100}; - Integer[] outputArray = cocktailShakerSort.sort(inputArray); - Integer[] expectedOutput = {-100, -33, 1, 2, 5, 27, 33, 78, 92, 99, 100}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void cocktailShakerSortStringArray() { - String[] inputArray = { - "g3x1", - "dN62", - "oMdr", - "KL2b", - "JddJ", - "mvE8", - "Ej7Q", - "n7n7", - "LGTg", - "2E1w", - }; - String[] outputArray = cocktailShakerSort.sort(inputArray); - String[] expectedOutput = { - "2E1w", - "Ej7Q", - "JddJ", - "KL2b", - "LGTg", - "dN62", - "g3x1", - "mvE8", - "n7n7", - "oMdr", - }; - assertArrayEquals(outputArray, expectedOutput); +public class CocktailShakerSortTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new CocktailShakerSort(); } }