refactor: cleanup PancakeSort (#5295)

* refactor: PancakeSort cleanup, changing test to standard

* checkstyle: fix formatting

---------

Co-authored-by: alxklm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-07-22 09:20:59 +02:00
committed by GitHub
parent 97d416e64e
commit 08db744240
2 changed files with 30 additions and 123 deletions

View File

@ -10,56 +10,35 @@ public class PancakeSort implements SortAlgorithm {
@Override @Override
public <T extends Comparable<T>> T[] sort(T[] array) { public <T extends Comparable<T>> T[] sort(T[] array) {
int size = array.length; if (array.length < 2) {
for (int i = 0; i < size; i++) {
T max = array[0];
int index = 0;
for (int j = 0; j < size - i; j++) {
if (SortUtils.less(max, array[j])) {
max = array[j];
index = j;
}
}
SortUtils.flip(array, index, array.length - 1 - i);
}
return array; return array;
} }
public static void main(String[] args) { for (int currentSize = 0; currentSize < array.length; currentSize++) {
Integer[] arr = { int maxIndex = findMaxIndex(array, currentSize);
10, SortUtils.flip(array, maxIndex, array.length - 1 - currentSize);
9, }
8,
7, return array;
6, }
15,
14, /**
7, * Finds the index of the maximum element in the array up to the given size.
4, *
3, * @param array the array to be searched
8, * @param currentSize the current size of the unsorted portion of the array
6, * @param <T> the type of elements in the array
3, * @return the index of the maximum element
1, */
2, private <T extends Comparable<T>> int findMaxIndex(T[] array, int currentSize) {
-2, T max = array[0];
-5, int maxIndex = 0;
-8, for (int i = 0; i < array.length - currentSize; i++) {
-3, if (SortUtils.less(max, array[i])) {
-1, max = array[i];
13, maxIndex = i;
12, }
11, }
5, return maxIndex;
4,
3,
2,
1,
};
PancakeSort pancakeSort = new PancakeSort();
System.out.println("After sorting:");
pancakeSort.sort(arr);
SortUtils.print(arr);
} }
} }

View File

@ -1,80 +1,8 @@
package com.thealgorithms.sorts; package com.thealgorithms.sorts;
import static org.assertj.core.api.Assertions.assertThat; public class PancakeSortTest extends SortingAlgorithmTest {
@Override
import org.junit.jupiter.api.DisplayName; SortAlgorithm getSortAlgorithm() {
import org.junit.jupiter.api.Test; return new PancakeSort();
public class PancakeSortTest {
private PancakeSort pancakeSort = new PancakeSort();
@Test
@DisplayName("Empty Array pancakeSort")
public void pancakeSortEmptyArray() {
Integer[] inputArray = {};
Integer[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEmpty();
}
@Test
@DisplayName("PancakeSort single Integer Array")
public void pancakeSort() {
Integer[] inputArray = {2};
Integer[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEqualTo(inputArray);
}
@Test
@DisplayName("PancakeSort non duplicate Integer Array")
public void pancakeSortNonDuplicateIntegerArray() {
Integer[] inputArray = {2, 1, 77, 34, 14, 56, 8};
Integer[] expectedOutput = {1, 2, 8, 14, 34, 56, 77};
Integer[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEqualTo(expectedOutput);
}
@Test
@DisplayName("PancakeSort Integer Array with duplicates")
public void pancakeSortDuplicateIntegerArray() {
Integer[] inputArray = {2, 1, 77, 34, 14, 77, 56, 14, 8};
Integer[] expectedOutput = {1, 2, 8, 14, 14, 34, 56, 77, 77};
Integer[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEqualTo(expectedOutput);
}
@Test
@DisplayName("PancakeSort negative Integer Array with duplicates")
public void pancakeSortNegativeDuplicateIntegerArray() {
Integer[] inputArray = {2, 1, 77, -34, -14, 77, 56, -14, 8};
Integer[] expectedOutput = {-34, -14, -14, 1, 2, 8, 56, 77, 77};
Integer[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEqualTo(expectedOutput);
}
@Test
@DisplayName("PancakeSort single String Array")
public void pancakeSortSingleStringArray() {
String[] inputArray = {"W"};
String[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEqualTo(inputArray);
}
@Test
@DisplayName("PancakeSort non duplicate String Array")
public void pancakeSortNonDuplicateStringArray() {
String[] inputArray = {"W", "A", "d", "be", "jk", "hb", "bgh"};
String[] expectedOutput = {"A", "W", "be", "bgh", "d", "hb", "jk"};
String[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEqualTo(expectedOutput);
}
@Test
@DisplayName("PancakeSort String Array with duplicates")
public void pancakeSortDuplicateStringArray() {
String[] inputArray = {"W", "A", "d", "be", "jk", "hb", "bgh", "bgh", "W"};
String[] expectedOutput = {"A", "W", "W", "be", "bgh", "bgh", "d", "hb", "jk"};
String[] outputArray = pancakeSort.sort(inputArray);
assertThat(outputArray).isEqualTo(expectedOutput);
} }
} }