refactor: BubbleSortRecursion: improving naming, adding standard test (#5267)

* refactor: improving naming, adding standard test

* style: remove `BubbleSortRecursive` from pmd exclude list

* docs: typo fix

---------

Co-authored-by: Alex Klymenko <alx@alx.com>
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
Alex Klymenko 2024-06-30 20:51:11 +03:00 committed by GitHub
parent 758df7dcc3
commit 208e1e99f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 45 additions and 59 deletions

View File

@ -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)

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,8 @@
package com.thealgorithms.sorts;
public class BubbleSortRecursiveTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new BubbleSortRecursive();
}
}