Skip to content

Commit 66bfaff

Browse files
authored
refactor: cleanup CocktailShakerSort (#5317)
1 parent 554b6cf commit 66bfaff

File tree

2 files changed

+63
-99
lines changed

2 files changed

+63
-99
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,81 @@
11
package com.thealgorithms.sorts;
22

33
/**
4+
* CocktailShakerSort class implements the Cocktail Shaker Sort algorithm,
5+
* which is a bidirectional bubble sort. It sorts the array by passing
6+
* through it back and forth, progressively moving the largest elements
7+
* to the end and the smallest elements to the beginning.
8+
*
49
* @author Mateus Bizzo (https://github.com/MattBizzo)
510
* @author Podshivalov Nikita (https://github.com/nikitap492)
611
*/
712
class CocktailShakerSort implements SortAlgorithm {
813

914
/**
10-
* This method implements the Generic Cocktail Shaker Sort
15+
* Sorts the given array using the Cocktail Shaker Sort algorithm.
1116
*
12-
* @param array The array to be sorted Sorts the array in increasing order
17+
* @param <T> The type of elements in the array, which must be comparable
18+
* @param array The array to be sorted
19+
* @return The sorted array
1320
*/
1421
@Override
15-
public <T extends Comparable<T>> T[] sort(T[] array) {
16-
int length = array.length;
22+
public <T extends Comparable<T>> T[] sort(final T[] array) {
23+
if (array.length == 0) {
24+
return array;
25+
}
26+
1727
int left = 0;
18-
int right = length - 1;
19-
int swappedLeft;
20-
int swappedRight;
28+
int right = array.length - 1;
29+
2130
while (left < right) {
22-
// front
23-
swappedRight = 0;
24-
for (int i = left; i < right; i++) {
25-
if (SortUtils.less(array[i + 1], array[i])) {
26-
SortUtils.swap(array, i, i + 1);
27-
swappedRight = i;
28-
}
29-
}
30-
// back
31-
right = swappedRight;
32-
swappedLeft = length - 1;
33-
for (int j = right; j > left; j--) {
34-
if (SortUtils.less(array[j], array[j - 1])) {
35-
SortUtils.swap(array, j - 1, j);
36-
swappedLeft = j;
37-
}
38-
}
39-
left = swappedLeft;
31+
right = performForwardPass(array, left, right);
32+
left = performBackwardPass(array, left, right);
4033
}
34+
4135
return array;
4236
}
4337

44-
// Driver Program
45-
public static void main(String[] args) {
46-
// Integer Input
47-
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
48-
CocktailShakerSort shakerSort = new CocktailShakerSort();
38+
/**
39+
* Performs a forward pass through the array, moving larger elements to the end.
40+
*
41+
* @param <T> The type of elements in the array, which must be comparable
42+
* @param array The array being sorted
43+
* @param left The current left boundary of the sorting area
44+
* @param right The current right boundary of the sorting area
45+
* @return The index of the last swapped element during this pass
46+
*/
47+
private <T extends Comparable<T>> int performForwardPass(final T[] array, final int left, final int right) {
48+
int lastSwappedIndex = left;
49+
50+
for (int i = left; i < right; i++) {
51+
if (SortUtils.less(array[i + 1], array[i])) {
52+
SortUtils.swap(array, i, i + 1);
53+
lastSwappedIndex = i;
54+
}
55+
}
56+
57+
return lastSwappedIndex;
58+
}
59+
60+
/**
61+
* Performs a backward pass through the array, moving smaller elements to the beginning.
62+
*
63+
* @param <T> The type of elements in the array, which must be comparable
64+
* @param array The array being sorted
65+
* @param left The current left boundary of the sorting area
66+
* @param right The current right boundary of the sorting area
67+
* @return The index of the last swapped element during this pass
68+
*/
69+
private <T extends Comparable<T>> int performBackwardPass(final T[] array, final int left, final int right) {
70+
int lastSwappedIndex = right;
4971

50-
// Output => 1 4 6 9 12 23 54 78 231
51-
SortUtils.print(shakerSort.sort(integers));
72+
for (int i = right; i > left; i--) {
73+
if (SortUtils.less(array[i], array[i - 1])) {
74+
SortUtils.swap(array, i - 1, i);
75+
lastSwappedIndex = i;
76+
}
77+
}
5278

53-
// String Input
54-
String[] strings = {"c", "a", "e", "b", "d"};
55-
SortUtils.print(shakerSort.sort(strings));
79+
return lastSwappedIndex;
5680
}
5781
}
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,8 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
5-
import org.junit.jupiter.api.Test;
6-
7-
/**
8-
* @author Tabbygray (https://github.com/Tabbygray)
9-
* @see CocktailShakerSort
10-
*/
11-
public class CocktailShakerSortTest {
12-
13-
private CocktailShakerSort cocktailShakerSort = new CocktailShakerSort();
14-
15-
@Test
16-
public void cocktailShakerSortEmptyArray() {
17-
Integer[] inputArray = {};
18-
Integer[] outputArray = cocktailShakerSort.sort(inputArray);
19-
Integer[] expectedOutput = {};
20-
assertArrayEquals(outputArray, expectedOutput);
21-
}
22-
23-
@Test
24-
public void cocktailShakerSortSingleStringElementArray() {
25-
String[] inputArray = {"Test"};
26-
String[] outputArray = cocktailShakerSort.sort(inputArray);
27-
String[] expectedOutput = {"Test"};
28-
assertArrayEquals(outputArray, expectedOutput);
29-
}
30-
31-
@Test
32-
public void cocktailShakerSortIntegerArray() {
33-
Integer[] inputArray = {2, 92, 1, 33, -33, 27, 5, 100, 78, 99, -100};
34-
Integer[] outputArray = cocktailShakerSort.sort(inputArray);
35-
Integer[] expectedOutput = {-100, -33, 1, 2, 5, 27, 33, 78, 92, 99, 100};
36-
assertArrayEquals(outputArray, expectedOutput);
37-
}
38-
39-
@Test
40-
public void cocktailShakerSortStringArray() {
41-
String[] inputArray = {
42-
"g3x1",
43-
"dN62",
44-
"oMdr",
45-
"KL2b",
46-
"JddJ",
47-
"mvE8",
48-
"Ej7Q",
49-
"n7n7",
50-
"LGTg",
51-
"2E1w",
52-
};
53-
String[] outputArray = cocktailShakerSort.sort(inputArray);
54-
String[] expectedOutput = {
55-
"2E1w",
56-
"Ej7Q",
57-
"JddJ",
58-
"KL2b",
59-
"LGTg",
60-
"dN62",
61-
"g3x1",
62-
"mvE8",
63-
"n7n7",
64-
"oMdr",
65-
};
66-
assertArrayEquals(outputArray, expectedOutput);
3+
public class CocktailShakerSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new CocktailShakerSort();
677
}
688
}

0 commit comments

Comments
 (0)