Skip to content

Commit a5be829

Browse files
authored
Merge branch 'master' into refactor/introspective_sort
2 parents 98149f6 + 66bfaff commit a5be829

11 files changed

+114
-194
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
}

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

+11-11
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

+5-48
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

+2-2
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])) {

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2525
* @param right The last index of an array
2626
* @param array The array to be sorted
2727
*/
28-
private static <T extends Comparable<T>> void doSort(T[] array, int left, int right) {
28+
private static <T extends Comparable<T>> void doSort(T[] array, final int left, final int right) {
2929
if (left < right) {
30-
int pivot = randomPartition(array, left, right);
30+
final int pivot = randomPartition(array, left, right);
3131
doSort(array, left, pivot - 1);
3232
doSort(array, pivot, right);
3333
}
@@ -41,8 +41,8 @@ private static <T extends Comparable<T>> void doSort(T[] array, int left, int ri
4141
* @param right The last index of an array
4242
* @return the partition index of the array
4343
*/
44-
private static <T extends Comparable<T>> int randomPartition(T[] array, int left, int right) {
45-
int randomIndex = left + (int) (Math.random() * (right - left + 1));
44+
private static <T extends Comparable<T>> int randomPartition(T[] array, final int left, final int right) {
45+
final int randomIndex = left + (int) (Math.random() * (right - left + 1));
4646
SortUtils.swap(array, randomIndex, right);
4747
return partition(array, left, right);
4848
}
@@ -56,8 +56,8 @@ private static <T extends Comparable<T>> int randomPartition(T[] array, int left
5656
* array
5757
*/
5858
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
59-
int mid = (left + right) >>> 1;
60-
T pivot = array[mid];
59+
final int mid = (left + right) >>> 1;
60+
final T pivot = array[mid];
6161

6262
while (left <= right) {
6363
while (SortUtils.less(array[left], pivot)) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2727
* @param index the current index to start sorting from
2828
* @param <T> the type of elements in the array (must be Comparable)
2929
*/
30-
private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array, int index) {
30+
private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array, final int index) {
3131
if (index == array.length - 1) {
3232
return;
3333
}
@@ -46,7 +46,7 @@ private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array,
4646
* @param <T> the type of elements in the array
4747
* @return the index of the minimum element
4848
*/
49-
private static <T extends Comparable<T>> int findMinIndex(T[] array, int start) {
49+
private static <T extends Comparable<T>> int findMinIndex(T[] array, final int start) {
5050
// Base case: if start is the last index, return start
5151
if (start == array.length - 1) {
5252
return start;

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
1515
return array;
1616
}
1717

18-
public <T extends Comparable<T>> T[] sort(T[] unsortedArray, int start, int end) {
19-
if (SortUtils.less(unsortedArray[end - 1], unsortedArray[start])) {
20-
T temp = unsortedArray[start];
21-
unsortedArray[start] = unsortedArray[end - 1];
22-
unsortedArray[end - 1] = temp;
18+
public <T extends Comparable<T>> T[] sort(final T[] array, final int start, final int end) {
19+
if (SortUtils.less(array[end - 1], array[start])) {
20+
final T temp = array[start];
21+
array[start] = array[end - 1];
22+
array[end - 1] = temp;
2323
}
2424

25-
int len = end - start;
26-
if (len > 2) {
27-
int third = len / 3;
28-
sort(unsortedArray, start, end - third);
29-
sort(unsortedArray, start + third, end);
30-
sort(unsortedArray, start, end - third);
25+
final int length = end - start;
26+
if (length > 2) {
27+
int third = length / 3;
28+
sort(array, start, end - third);
29+
sort(array, start + third, end);
30+
sort(array, start, end - third);
3131
}
32-
return unsortedArray;
32+
return array;
3333
}
3434
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public final class StrandSort implements SortAlgorithm {
1313
* Sorts the given array using the Strand Sort algorithm.
1414
*
1515
* @param <T> The type of elements to be sorted, must be Comparable.
16-
* @param unsorted The array to be sorted.
16+
* @param array The array to be sorted.
1717
* @return The sorted array.
1818
*/
1919
@Override
20-
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
21-
List<T> unsortedList = new ArrayList<>(Arrays.asList(unsorted));
20+
public <T extends Comparable<T>> T[] sort(T[] array) {
21+
List<T> unsortedList = new ArrayList<>(Arrays.asList(array));
2222
List<T> sortedList = strandSort(unsortedList);
23-
return sortedList.toArray(unsorted);
23+
return sortedList.toArray(array);
2424
}
2525

2626
/**

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ public class SwapSort implements SortAlgorithm {
1111

1212
@Override
1313
public <T extends Comparable<T>> T[] sort(T[] array) {
14-
int len = array.length;
1514
int index = 0;
1615

17-
while (index < len - 1) {
18-
int amountSmallerElements = this.getSmallerElementCount(array, index);
16+
while (index < array.length - 1) {
17+
final int amountSmallerElements = this.getSmallerElementCount(array, index);
1918

2019
if (amountSmallerElements > 0) {
2120
SortUtils.swap(array, index, index + amountSmallerElements);
@@ -27,7 +26,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2726
return array;
2827
}
2928

30-
private <T extends Comparable<T>> int getSmallerElementCount(T[] array, int index) {
29+
private <T extends Comparable<T>> int getSmallerElementCount(final T[] array, final int index) {
3130
int counter = 0;
3231
for (int i = index + 1; i < array.length; i++) {
3332
if (SortUtils.less(array[i], array[index])) {

0 commit comments

Comments
 (0)