From 208e1e99f0ced1a3f3b19371a4f198decf1aac50 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 30 Jun 2024 20:51:11 +0300 Subject: [PATCH] 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 Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- DIRECTORY.md | 3 +- pmd-exclude.properties | 1 - .../sorts/BubbleSortRecursion.java | 57 ------------------- .../sorts/BubbleSortRecursive.java | 35 ++++++++++++ .../sorts/BubbleSortRecursiveTest.java | 8 +++ 5 files changed, 45 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/sorts/BubbleSortRecursion.java create mode 100644 src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java create mode 100644 src/test/java/com/thealgorithms/sorts/BubbleSortRecursiveTest.java 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[] 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 > 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[] 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 > 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); + } + } +} 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(); + } +}