Skip to content

Commit 5d00889

Browse files
authored
fix: handle empty inputs in CircleSort (#5121)
* fix: handle empty inputs in `CircleSort` * style: remove `main` method
1 parent 6bde5d7 commit 5d00889

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

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

+3-17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class CircleSort implements SortAlgorithm {
1010
@Override
1111
public <T extends Comparable<T>> T[] sort(T[] array) {
1212
int n = array.length;
13+
if (n == 0) {
14+
return array;
15+
}
1316
while (doSort(array, 0, n - 1)) {
1417
}
1518
return array;
@@ -50,21 +53,4 @@ private <T extends Comparable<T>> Boolean doSort(T[] array, int left, int right)
5053

5154
return swapped || leftHalf || rightHalf;
5255
}
53-
54-
/* Driver code*/
55-
public static void main(String[] args) {
56-
CircleSort CSort = new CircleSort();
57-
58-
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
59-
CSort.sort(arr);
60-
for (int i = 0; i < arr.length - 1; ++i) {
61-
assert arr[i] <= arr[i + 1];
62-
}
63-
64-
String[] stringArray = {"c", "a", "e", "b", "d"};
65-
CSort.sort(stringArray);
66-
for (int i = 0; i < stringArray.length - 1; ++i) {
67-
assert arr[i].compareTo(arr[i + 1]) <= 0;
68-
}
69-
}
7056
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.thealgorithms.sorts;
2+
3+
class CircleSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new CircleSort();
7+
}
8+
}

0 commit comments

Comments
 (0)