mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
refactor: cleanup CocktailShakerSort
(#5317)
This commit is contained in:
@ -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 <T> 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 extends Comparable<T>> 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 extends Comparable<T>> 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 <T> 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 <T extends Comparable<T>> 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 <T> 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 <T extends Comparable<T>> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user