-
Notifications
You must be signed in to change notification settings - Fork 19.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
- Loading branch information
1 parent
758df7d
commit 208e1e9
Showing
5 changed files
with
45 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
src/main/java/com/thealgorithms/sorts/BubbleSortRecursion.java
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/java/com/thealgorithms/sorts/BubbleSortRecursiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |