Repository: TheAlgorithms/Java
Pull request #5283 (2024-07-11 18:36:11)

refactor: cleanup `StoogeSort`
Cleanup StoogeSort

Adding empty array check

Adding StoogeSortTest

<!--
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
-->

<!-- 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/5283?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 40.48%. Comparing base [(`8ea90fd`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/8ea90fdd428d281d85da1c656f75fca04f27df86?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`6e139dd`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6e139dd149ec4a9f80aaa359474c6da1957034fb?dropdown=coverage&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    #5283      +/-   ##
============================================
+ Coverage     40.35%   40.48%   +0.12%     
- Complexity     2486     2492       +6     
============================================
  Files           519      519              
  Lines         15479    15471       -8     
  Branches       2950     2950              
============================================
+ Hits           6247     6263      +16     
+ Misses         8943     8919      -24     
  Partials        289      289              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5283?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/DIRECTORY.md b/DIRECTORY.md
index ef7d163a..0c14970b 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -891,6 +891,7 @@
             * [SortingAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java)
             * [SortUtilsRandomGeneratorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SortUtilsRandomGeneratorTest.java)
             * [SortUtilsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SortUtilsTest.java)
+            * [StoogeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/StoogeSortTest.java)
             * [StrandSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/StrandSortTest.java)
             * [SwapSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SwapSortTest.java)
             * [TimSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/TimSortTest.java)
diff --git a/src/main/java/com/thealgorithms/sorts/StoogeSort.java b/src/main/java/com/thealgorithms/sorts/StoogeSort.java
index 330f9752..25830109 100644
--- a/src/main/java/com/thealgorithms/sorts/StoogeSort.java
+++ b/src/main/java/com/thealgorithms/sorts/StoogeSort.java
@@ -7,9 +7,12 @@ package com.thealgorithms.sorts;
 public class StoogeSort implements SortAlgorithm {
 
     @Override
-    public <T extends Comparable<T>> T[] sort(T[] unsortedArray) {
-        sort(unsortedArray, 0, unsortedArray.length);
-        return unsortedArray;
+    public <T extends Comparable<T>> T[] sort(T[] array) {
+        if (array.length == 0) {
+            return array;
+        }
+        sort(array, 0, array.length);
+        return array;
     }
 
     public <T extends Comparable<T>> T[] sort(T[] unsortedArray, int start, int end) {
@@ -28,24 +31,4 @@ public class StoogeSort implements SortAlgorithm {
         }
         return unsortedArray;
     }
-
-    public static void main(String[] args) {
-        StoogeSort stoogeSort = new StoogeSort();
-
-        Integer[] integerArray = {8, 84, 53, 953, 64, 2, 202};
-        // Print integerArray unsorted
-        SortUtils.print(integerArray);
-
-        stoogeSort.sort(integerArray);
-        // Print integerArray sorted
-        SortUtils.print(integerArray);
-
-        String[] stringArray = {"g", "d", "a", "b", "f", "c", "e"};
-        // Print stringArray unsorted
-        SortUtils.print(stringArray);
-
-        stoogeSort.sort(stringArray);
-        // Print stringArray sorted
-        SortUtils.print(stringArray);
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/StoogeSortTest.java b/src/test/java/com/thealgorithms/sorts/StoogeSortTest.java
new file mode 100644
index 00000000..e230ac2a
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/StoogeSortTest.java
@@ -0,0 +1,12 @@
+package com.thealgorithms.sorts;
+
+public class StoogeSortTest extends SortingAlgorithmTest {
+    protected int getGeneratedArraySize() {
+        return 1000;
+    }
+
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return new StoogeSort();
+    }
+}