Skip to content

Commit b9c3475

Browse files
authored
Merge branch 'master' into refactor/dualpivotquicksort
2 parents 90126e6 + 2837585 commit b9c3475

13 files changed

+207
-287
lines changed
Lines changed: 59 additions & 35 deletions
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
}

src/main/java/com/thealgorithms/sorts/DutchNationalFlagSort.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@
1212
public class DutchNationalFlagSort implements SortAlgorithm {
1313

1414
@Override
15-
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
16-
return dutchNationalFlagSort(unsorted, unsorted[(int) Math.ceil((unsorted.length) / 2.0) - 1]);
15+
public <T extends Comparable<T>> T[] sort(T[] array) {
16+
return dutchNationalFlagSort(array, array[(int) Math.ceil((array.length) / 2.0) - 1]);
1717
}
1818

19-
public <T extends Comparable<T>> T[] sort(T[] unsorted, T intendedMiddle) {
20-
return dutchNationalFlagSort(unsorted, intendedMiddle);
19+
public <T extends Comparable<T>> T[] sort(T[] array, T intendedMiddle) {
20+
return dutchNationalFlagSort(array, intendedMiddle);
2121
}
2222

23-
private <T extends Comparable<T>> T[] dutchNationalFlagSort(T[] arr, T intendedMiddle) {
23+
private <T extends Comparable<T>> T[] dutchNationalFlagSort(final T[] array, final T intendedMiddle) {
2424
int i = 0;
2525
int j = 0;
26-
int k = arr.length - 1;
26+
int k = array.length - 1;
2727

2828
while (j <= k) {
29-
if (0 > arr[j].compareTo(intendedMiddle)) {
30-
SortUtils.swap(arr, i, j);
29+
if (0 > array[j].compareTo(intendedMiddle)) {
30+
SortUtils.swap(array, i, j);
3131
j++;
3232
i++;
33-
} else if (0 < arr[j].compareTo(intendedMiddle)) {
34-
SortUtils.swap(arr, j, k);
33+
} else if (0 < array[j].compareTo(intendedMiddle)) {
34+
SortUtils.swap(array, j, k);
3535
k--;
3636
} else {
3737
j++;
3838
}
3939
}
40-
return arr;
40+
return array;
4141
}
4242
}

src/main/java/com/thealgorithms/sorts/GnomeSort.java

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,20 @@
99
public class GnomeSort implements SortAlgorithm {
1010

1111
@Override
12-
public <T extends Comparable<T>> T[] sort(T[] arr) {
12+
public <T extends Comparable<T>> T[] sort(final T[] array) {
1313
int i = 1;
1414
int j = 2;
15-
while (i < arr.length) {
16-
if (SortUtils.less(arr[i - 1], arr[i])) {
15+
while (i < array.length) {
16+
if (SortUtils.less(array[i - 1], array[i])) {
1717
i = j++;
1818
} else {
19-
SortUtils.swap(arr, i - 1, i);
19+
SortUtils.swap(array, i - 1, i);
2020
if (--i == 0) {
2121
i = j++;
2222
}
2323
}
2424
}
2525

26-
return null;
27-
}
28-
29-
public static void main(String[] args) {
30-
Integer[] integers = {
31-
4,
32-
23,
33-
6,
34-
78,
35-
1,
36-
26,
37-
11,
38-
23,
39-
0,
40-
-6,
41-
3,
42-
54,
43-
231,
44-
9,
45-
12,
46-
};
47-
String[] strings = {
48-
"c",
49-
"a",
50-
"e",
51-
"b",
52-
"d",
53-
"dd",
54-
"da",
55-
"zz",
56-
"AA",
57-
"aa",
58-
"aB",
59-
"Hb",
60-
"Z",
61-
};
62-
GnomeSort gnomeSort = new GnomeSort();
63-
64-
gnomeSort.sort(integers);
65-
gnomeSort.sort(strings);
66-
67-
System.out.println("After sort : ");
68-
SortUtils.print(integers);
69-
SortUtils.print(strings);
26+
return array;
7027
}
7128
}

src/main/java/com/thealgorithms/sorts/HeapSort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2525
return array;
2626
}
2727

28-
private static <T extends Comparable<T>> void heapify(T[] array, int n) {
28+
private <T extends Comparable<T>> void heapify(final T[] array, final int n) {
2929
for (int k = n / 2; k >= 1; k--) {
3030
siftDown(array, k, n);
3131
}
3232
}
3333

34-
private static <T extends Comparable<T>> void siftDown(T[] array, int k, int n) {
34+
private <T extends Comparable<T>> void siftDown(final T[] array, int k, final int n) {
3535
while (2 * k <= n) {
3636
int j = 2 * k;
3737
if (j < n && SortUtils.less(array[j - 1], array[j])) {

0 commit comments

Comments
 (0)