Skip to content

Commit

Permalink
refactor: BubbleSortRecursion: improving naming, adding standard test (
Browse files Browse the repository at this point in the history
…#5267)

* refactor: improving naming, adding standard test

* style: remove `BubbleSortRecursive` from pmd exclude list

* docs: typo fix

---------

Co-authored-by: Alex Klymenko <[email protected]>
Co-authored-by: Piotr Idzik <[email protected]>
  • Loading branch information
3 people authored Jun 30, 2024
1 parent 758df7d commit 208e1e9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 59 deletions.
3 changes: 2 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion pmd-exclude.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/com/thealgorithms/sorts/BubbleSortRecursion.java

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thealgorithms.sorts;

public class BubbleSortRecursiveTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new BubbleSortRecursive();
}
}

0 comments on commit 208e1e9

Please sign in to comment.