refactor: cleanup StoogeSort (#5283)

* refactor: cleanup StoogeSort

* refactor: update DIRECTORY.md for StoogeSortTest

---------

Co-authored-by: Alex Klymenko <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-07-20 21:28:06 +02:00 committed by GitHub
parent 94032148ca
commit 97d416e64e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 23 deletions

View File

@ -891,6 +891,7 @@
* [SortingAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SortingAlgorithmTest.java)
* [SortUtilsRandomGeneratorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SortUtilsRandomGeneratorTest.java)
* [SortUtilsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SortUtilsTest.java)
* [StoogeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/StoogeSortTest.java)
* [StrandSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/StrandSortTest.java)
* [SwapSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SwapSortTest.java)
* [TimSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/TimSortTest.java)

View File

@ -7,9 +7,12 @@ package com.thealgorithms.sorts;
public class StoogeSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] unsortedArray) {
sort(unsortedArray, 0, unsortedArray.length);
return unsortedArray;
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length == 0) {
return array;
}
sort(array, 0, array.length);
return array;
}
public <T extends Comparable<T>> T[] sort(T[] unsortedArray, int start, int end) {
@ -28,24 +31,4 @@ public class StoogeSort implements SortAlgorithm {
}
return unsortedArray;
}
public static void main(String[] args) {
StoogeSort stoogeSort = new StoogeSort();
Integer[] integerArray = {8, 84, 53, 953, 64, 2, 202};
// Print integerArray unsorted
SortUtils.print(integerArray);
stoogeSort.sort(integerArray);
// Print integerArray sorted
SortUtils.print(integerArray);
String[] stringArray = {"g", "d", "a", "b", "f", "c", "e"};
// Print stringArray unsorted
SortUtils.print(stringArray);
stoogeSort.sort(stringArray);
// Print stringArray sorted
SortUtils.print(stringArray);
}
}

View File

@ -0,0 +1,12 @@
package com.thealgorithms.sorts;
public class StoogeSortTest extends SortingAlgorithmTest {
protected int getGeneratedArraySize() {
return 1000;
}
@Override
SortAlgorithm getSortAlgorithm() {
return new StoogeSort();
}
}