Repository: TheAlgorithms/Java
Pull request #5121 (2024-04-26 06:49:56)

fix: handle empty inputs in `CircleSort`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

This PR adds tests for `CircleSort` and fixes the _infinite_ loop occurring for empty input.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5121?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 37.49%. Comparing base [(`6de154d`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6de154d218a54186dfc8efefc45aab862b086aee?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`b3de373`)](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5121?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5121      +/-   ##
============================================
+ Coverage     37.35%   37.49%   +0.14%     
- Complexity     2350     2363      +13     
============================================
  Files           517      517              
  Lines         15623    15625       +2     
  Branches       2971     2972       +1     
============================================
+ Hits           5836     5859      +23     
+ Misses         9500     9478      -22     
- Partials        287      288       +1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5121?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/CircleSort.java b/src/main/java/com/thealgorithms/sorts/CircleSort.java
index 7f2dc5fd..c6a6d9ea 100644
--- a/src/main/java/com/thealgorithms/sorts/CircleSort.java
+++ b/src/main/java/com/thealgorithms/sorts/CircleSort.java
@@ -10,6 +10,9 @@ public class CircleSort implements SortAlgorithm {
     @Override
     public <T extends Comparable<T>> T[] sort(T[] array) {
         int n = array.length;
+        if (n == 0) {
+            return array;
+        }
         while (doSort(array, 0, n - 1)) {
         }
         return array;
@@ -50,21 +53,4 @@ public class CircleSort implements SortAlgorithm {
 
         return swapped || leftHalf || rightHalf;
     }
-
-    /* Driver code*/
-    public static void main(String[] args) {
-        CircleSort CSort = new CircleSort();
-
-        Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
-        CSort.sort(arr);
-        for (int i = 0; i < arr.length - 1; ++i) {
-            assert arr[i] <= arr[i + 1];
-        }
-
-        String[] stringArray = {"c", "a", "e", "b", "d"};
-        CSort.sort(stringArray);
-        for (int i = 0; i < stringArray.length - 1; ++i) {
-            assert arr[i].compareTo(arr[i + 1]) <= 0;
-        }
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/CircleSortTest.java b/src/test/java/com/thealgorithms/sorts/CircleSortTest.java
new file mode 100644
index 00000000..d1134732
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/CircleSortTest.java
@@ -0,0 +1,8 @@
+package com.thealgorithms.sorts;
+
+class CircleSortTest extends SortingAlgorithmTest {
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return new CircleSort();
+    }
+}