Skip to content

Commit 41f76e0

Browse files
authored
refactor: simple improvements and cleanup for different sorts (#5320)
1 parent 2837585 commit 41f76e0

File tree

6 files changed

+54
-135
lines changed

6 files changed

+54
-135
lines changed

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@ public class CircleSort implements SortAlgorithm {
77
*/
88
@Override
99
public <T extends Comparable<T>> T[] sort(T[] array) {
10-
int n = array.length;
11-
if (n == 0) {
10+
if (array.length == 0) {
1211
return array;
1312
}
14-
while (doSort(array, 0, n - 1)) {
13+
while (doSort(array, 0, array.length - 1)) {
1514
}
1615
return array;
1716
}
1817

19-
/* This method implements the cyclic sort recursive version
18+
/**
19+
* Recursively sorts the array in a circular manner by comparing elements
20+
* from the start and end of the current segment.
21+
*
22+
* @param <T> The type of elements in the array, which must be comparable
2023
* @param array The array to be sorted
21-
* @param the left boundary of the part currently being sorted
22-
* @param the right boundary of the part currently being sorted
24+
* @param left The left boundary of the current segment being sorted
25+
* @param right The right boundary of the current segment being sorted
26+
* @return true if any elements were swapped during the sort; false otherwise
2327
*/
24-
private <T extends Comparable<T>> Boolean doSort(T[] array, int left, int right) {
28+
private <T extends Comparable<T>> boolean doSort(final T[] array, final int left, final int right) {
2529
boolean swapped = false;
2630

2731
if (left == right) {
28-
return Boolean.FALSE;
32+
return false;
2933
}
3034

3135
int low = left;
@@ -45,10 +49,10 @@ private <T extends Comparable<T>> Boolean doSort(T[] array, int left, int right)
4549
swapped = true;
4650
}
4751

48-
int mid = left + (right - left) / 2;
49-
Boolean leftHalf = doSort(array, left, mid);
50-
Boolean rightHalf = doSort(array, mid + 1, right);
52+
final int mid = left + (right - left) / 2;
53+
final boolean leftHalfSwapped = doSort(array, left, mid);
54+
final boolean rightHalfSwapped = doSort(array, mid + 1, right);
5155

52-
return swapped || leftHalf || rightHalf;
56+
return swapped || leftHalfSwapped || rightHalfSwapped;
5357
}
5458
}

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

+24-14
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,39 @@ public final class OddEvenSort implements SortAlgorithm {
1313
* Sorts the given array using the Odd-Even Sort algorithm.
1414
*
1515
* @param <T> the type of elements in the array, which must implement the Comparable interface
16-
* @param arr 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[] arr) {
20+
public <T extends Comparable<T>> T[] sort(T[] array) {
2121
boolean sorted = false;
2222
while (!sorted) {
23-
sorted = true;
23+
sorted = performOddSort(array);
24+
sorted = performEvenSort(array) && sorted;
25+
}
26+
27+
return array;
28+
}
2429

25-
for (int i = 1; i < arr.length - 1; i += 2) {
26-
if (arr[i].compareTo(arr[i + 1]) > 0) {
27-
SortUtils.swap(arr, i, i + 1);
28-
sorted = false;
29-
}
30+
private <T extends Comparable<T>> boolean performOddSort(T[] array) {
31+
boolean sorted = true;
32+
for (int i = 1; i < array.length - 1; i += 2) {
33+
if (array[i].compareTo(array[i + 1]) > 0) {
34+
SortUtils.swap(array, i, i + 1);
35+
sorted = false;
3036
}
37+
}
38+
return sorted;
39+
}
3140

32-
for (int i = 0; i < arr.length - 1; i += 2) {
33-
if (arr[i].compareTo(arr[i + 1]) > 0) {
34-
SortUtils.swap(arr, i, i + 1);
35-
sorted = false;
36-
}
41+
private <T extends Comparable<T>> boolean performEvenSort(T[] array) {
42+
boolean sorted = true;
43+
for (int i = 0; i < array.length - 1; i += 2) {
44+
if (array[i].compareTo(array[i + 1]) > 0) {
45+
SortUtils.swap(array, i, i + 1);
46+
sorted = false;
3747
}
3848
}
39-
return arr;
49+
return sorted;
4050
}
4151
}

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ public class SelectionSort implements SortAlgorithm {
1010
*/
1111
@Override
1212
public <T extends Comparable<T>> T[] sort(T[] array) {
13-
// One by one move the boundary of the unsorted subarray
14-
for (int i = 0; i < array.length - 1; i++) {
1513

16-
// Swap the remaining minimum element with the current element
17-
SortUtils.swap(array, i, findIndexOfMin(array, i));
14+
for (int i = 0; i < array.length - 1; i++) {
15+
final int minIndex = findIndexOfMin(array, i);
16+
SortUtils.swap(array, i, minIndex);
1817
}
1918
return array;
2019
}
2120

22-
private static <T extends Comparable<T>> int findIndexOfMin(T[] array, final int start) {
23-
int minIndex = start;
24-
for (int i = start + 1; i < array.length; i++) {
21+
private static <T extends Comparable<T>> int findIndexOfMin(T[] array, final int startIndex) {
22+
int minIndex = startIndex;
23+
for (int i = startIndex + 1; i < array.length; i++) {
2524
if (array[i].compareTo(array[minIndex]) < 0) {
2625
minIndex = i;
2726
}
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,15 @@
11
package com.thealgorithms.sorts;
22

33
public class SimpleSort implements SortAlgorithm {
4-
54
@Override
65
public <T extends Comparable<T>> T[] sort(T[] array) {
7-
final int length = array.length;
8-
9-
for (int i = 0; i < length; i++) {
10-
for (int j = i + 1; j < length; j++) {
6+
for (int i = 0; i < array.length; i++) {
7+
for (int j = i + 1; j < array.length; j++) {
118
if (SortUtils.less(array[j], array[i])) {
129
SortUtils.swap(array, i, j);
1310
}
1411
}
1512
}
16-
1713
return array;
1814
}
19-
20-
public static void main(String[] args) {
21-
// ==== Int =======
22-
Integer[] a = {3, 7, 45, 1, 33, 5, 2, 9};
23-
System.out.print("unsorted: ");
24-
SortUtils.print(a);
25-
System.out.println();
26-
27-
new SimpleSort().sort(a);
28-
System.out.print("sorted: ");
29-
SortUtils.print(a);
30-
System.out.println();
31-
32-
// ==== String =======
33-
String[] b = {
34-
"banana",
35-
"berry",
36-
"orange",
37-
"grape",
38-
"peach",
39-
"cherry",
40-
"apple",
41-
"pineapple",
42-
};
43-
System.out.print("unsorted: ");
44-
SortUtils.print(b);
45-
System.out.println();
46-
47-
new SimpleSort().sort(b);
48-
System.out.print("sorted: ");
49-
SortUtils.print(b);
50-
}
5115
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ private static <T extends Comparable<? super T>> List<T> strandSort(List<T> list
3838
List<T> result = new ArrayList<>();
3939
while (!list.isEmpty()) {
4040
final List<T> sorted = new ArrayList<>();
41-
sorted.add(list.remove(0));
41+
sorted.add(list.removeFirst());
4242
for (int i = 0; i < list.size();) {
43-
if (sorted.get(sorted.size() - 1).compareTo(list.get(i)) <= 0) {
43+
if (sorted.getLast().compareTo(list.get(i)) <= 0) {
4444
sorted.add(list.remove(i));
4545
} else {
4646
i++;
Original file line numberDiff line numberDiff line change
@@ -1,66 +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-
public class SimpleSortTest {
8-
9-
private SimpleSort simpleSort = new SimpleSort();
10-
11-
@Test
12-
public void simpleSortEmptyArray() {
13-
Integer[] inputArray = {};
14-
Integer[] outputArray = simpleSort.sort(inputArray);
15-
Integer[] expectedOutput = {};
16-
assertArrayEquals(outputArray, expectedOutput);
17-
}
18-
19-
@Test
20-
public void simpleSortSingleIntegerArray() {
21-
Integer[] inputArray = {4};
22-
Integer[] outputArray = simpleSort.sort(inputArray);
23-
Integer[] expectedOutput = {4};
24-
assertArrayEquals(outputArray, expectedOutput);
25-
}
26-
27-
@Test
28-
public void simpleSortSingleStringArray() {
29-
String[] inputArray = {"s"};
30-
String[] outputArray = simpleSort.sort(inputArray);
31-
String[] expectedOutput = {"s"};
32-
assertArrayEquals(outputArray, expectedOutput);
33-
}
34-
35-
@Test
36-
public void simpleSortNonDuplicateIntegerArray() {
37-
Integer[] inputArray = {6, -1, 99, 27, -15, 23, -36};
38-
Integer[] outputArray = simpleSort.sort(inputArray);
39-
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 27, 99};
40-
assertArrayEquals(outputArray, expectedOutput);
41-
}
42-
43-
@Test
44-
public void simpleSortDuplicateIntegerArray() {
45-
Integer[] inputArray = {6, -1, 27, -15, 23, 27, -36, 23};
46-
Integer[] outputArray = simpleSort.sort(inputArray);
47-
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 23, 27, 27};
48-
assertArrayEquals(outputArray, expectedOutput);
49-
}
50-
51-
@Test
52-
public void simpleSortNonDuplicateStringArray() {
53-
String[] inputArray = {"s", "b", "k", "a", "d", "c", "h"};
54-
String[] outputArray = simpleSort.sort(inputArray);
55-
String[] expectedOutput = {"a", "b", "c", "d", "h", "k", "s"};
56-
assertArrayEquals(outputArray, expectedOutput);
57-
}
58-
59-
@Test
60-
public void simpleSortDuplicateStringArray() {
61-
String[] inputArray = {"s", "b", "d", "a", "d", "c", "h", "b"};
62-
String[] outputArray = simpleSort.sort(inputArray);
63-
String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"};
64-
assertArrayEquals(outputArray, expectedOutput);
3+
public class SimpleSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new SimpleSort();
657
}
668
}

0 commit comments

Comments
 (0)