Repository: TheAlgorithms/Java
Pull request #5267 (2024-06-29 21:50:00)

refactor: cleanup `BubbleSortRecursion`
Renaming BubbleSortRecursion to BubbleSortRecursive.

Other improving naming.

Adding standard sort test class.

Adding standard javadocs.

<!--
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/5267?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.06%. Comparing base [(`758df7d`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/758df7dcc3cf8c34bd37618b2c1d2cad75bc8a51?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`b3232f7`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b3232f7b2e609227bb29639cab3eea588a069b6e?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    #5267      +/-   ##
============================================
+ Coverage     39.95%   40.06%   +0.10%     
- Complexity     2458     2465       +7     
============================================
  Files           519      519              
  Lines         15488    15479       -9     
  Branches       2957     2954       -3     
============================================
+ Hits           6189     6202      +13     
+ Misses         9008     8987      -21     
+ Partials        291      290       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5267?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 c033fe30..6cecfef0 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -490,7 +490,7 @@
             * [BitonicSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BitonicSort.java)
             * [BogoSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BogoSort.java)
             * [BubbleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BubbleSort.java)
-            * [BubbleSortRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BubbleSortRecursion.java)
+            * [BubbleSortRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java)
             * [BucketSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BucketSort.java)
             * [CircleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CircleSort.java)
             * [CocktailShakerSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CocktailShakerSort.java)
@@ -859,6 +859,7 @@
             * [BinaryInsertionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java)
             * [BogoSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BogoSortTest.java)
             * [BubbleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java)
+            * [BubbleSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BubbleSortRecursiveTest.java)
             * [BucketSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BucketSortTest.java)
             * [CircleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/CircleSortTest.java)
             * [CocktailShakerSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/CocktailShakerSortTest.java)
diff --git a/pmd-exclude.properties b/pmd-exclude.properties
index eb199da3..b7bc6f07 100644
--- a/pmd-exclude.properties
+++ b/pmd-exclude.properties
@@ -76,7 +76,6 @@ com.thealgorithms.searches.InterpolationSearch=UselessParentheses
 com.thealgorithms.searches.KMPSearch=UselessParentheses
 com.thealgorithms.searches.LinearSearchThread=EmptyCatchBlock
 com.thealgorithms.searches.RabinKarpAlgorithm=UselessParentheses
-com.thealgorithms.sorts.BubbleSortRecursion=UselessParentheses
 com.thealgorithms.sorts.CircleSort=EmptyControlStatement
 com.thealgorithms.sorts.CombSort=UselessParentheses
 com.thealgorithms.sorts.DutchNationalFlagSort=UselessParentheses
diff --git a/src/main/java/com/thealgorithms/sorts/BubbleSortRecursion.java b/src/main/java/com/thealgorithms/sorts/BubbleSortRecursion.java
deleted file mode 100644
index 10197969..00000000
--- a/src/main/java/com/thealgorithms/sorts/BubbleSortRecursion.java
+++ /dev/null
@@ -1,57 +0,0 @@
-package com.thealgorithms.sorts;
-
-import java.util.Random;
-
-/**
- * BubbleSort algorithm implements using recursion
- */
-public class BubbleSortRecursion implements SortAlgorithm {
-
-    public static void main(String[] args) {
-        Integer[] array = new Integer[10];
-
-        Random random = new Random();
-        /* generate 10 random numbers from -50 to 49 */
-        for (int i = 0; i < array.length; ++i) {
-            array[i] = random.nextInt(100) - 50;
-        }
-
-        BubbleSortRecursion bubbleSortRecursion = new BubbleSortRecursion();
-        bubbleSortRecursion.sort(array);
-
-        /* check array is sorted or not */
-        for (int i = 0; i < array.length - 1; ++i) {
-            assert (array[i].compareTo(array[i + 1]) <= 0);
-        }
-    }
-
-    /**
-     * @param unsorted - an array should be sorted
-     * @return sorted array
-     */
-    @Override
-    public <T extends Comparable<T>> T[] sort(T[] unsorted) {
-        bubbleSort(unsorted, unsorted.length);
-        return unsorted;
-    }
-
-    /**
-     * BubbleSort algorithm implements using recursion
-     *
-     * @param unsorted array contains elements
-     * @param len length of given array
-     */
-    private static <T extends Comparable<T>> void bubbleSort(T[] unsorted, int len) {
-        boolean swapped = false;
-        /* flag to check if array is sorted or not */
-        for (int i = 0; i < len - 1; ++i) {
-            if (SortUtils.greater(unsorted[i], unsorted[i + 1])) {
-                SortUtils.swap(unsorted, i, i + 1);
-                swapped = true;
-            }
-        }
-        if (swapped) {
-            bubbleSort(unsorted, len - 1);
-        }
-    }
-}
diff --git a/src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java b/src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java
new file mode 100644
index 00000000..d9cc00f9
--- /dev/null
+++ b/src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java
@@ -0,0 +1,35 @@
+package com.thealgorithms.sorts;
+
+/**
+ * BubbleSort algorithm implemented using recursion
+ */
+public class BubbleSortRecursive implements SortAlgorithm {
+    /**
+     * @param array - an array should be sorted
+     * @return sorted array
+     */
+    @Override
+    public <T extends Comparable<T>> T[] sort(T[] array) {
+        bubbleSort(array, array.length);
+        return array;
+    }
+
+    /**
+     * BubbleSort algorithm implements using recursion
+     *
+     * @param array array contains elements
+     * @param len length of given array
+     */
+    private static <T extends Comparable<T>> void bubbleSort(T[] array, int len) {
+        boolean swapped = false;
+        for (int i = 0; i < len - 1; ++i) {
+            if (SortUtils.greater(array[i], array[i + 1])) {
+                SortUtils.swap(array, i, i + 1);
+                swapped = true;
+            }
+        }
+        if (swapped) {
+            bubbleSort(array, len - 1);
+        }
+    }
+}


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