|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
3 | 3 | /**
|
| 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 | + * |
4 | 9 | * @author Mateus Bizzo (https://github.com/MattBizzo)
|
5 | 10 | * @author Podshivalov Nikita (https://github.com/nikitap492)
|
6 | 11 | */
|
7 | 12 | class CocktailShakerSort implements SortAlgorithm {
|
8 | 13 |
|
9 | 14 | /**
|
10 |
| - * This method implements the Generic Cocktail Shaker Sort |
| 15 | + * Sorts the given array using the Cocktail Shaker Sort algorithm. |
11 | 16 | *
|
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 |
13 | 20 | */
|
14 | 21 | @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 | + |
17 | 27 | int left = 0;
|
18 |
| - int right = length - 1; |
19 |
| - int swappedLeft; |
20 |
| - int swappedRight; |
| 28 | + int right = array.length - 1; |
| 29 | + |
21 | 30 | 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); |
40 | 33 | }
|
| 34 | + |
41 | 35 | return array;
|
42 | 36 | }
|
43 | 37 |
|
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; |
49 | 71 |
|
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 | + } |
52 | 78 |
|
53 |
| - // String Input |
54 |
| - String[] strings = {"c", "a", "e", "b", "d"}; |
55 |
| - SortUtils.print(shakerSort.sort(strings)); |
| 79 | + return lastSwappedIndex; |
56 | 80 | }
|
57 | 81 | }
|
0 commit comments