Skip to content

refactor: cleanup PancakeSort #5295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 25 additions & 46 deletions src/main/java/com/thealgorithms/sorts/PancakeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,35 @@ public class PancakeSort implements SortAlgorithm {

@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
int size = array.length;
if (array.length < 2) {
return array;
}

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);
for (int currentSize = 0; currentSize < array.length; currentSize++) {
int maxIndex = findMaxIndex(array, currentSize);
SortUtils.flip(array, maxIndex, array.length - 1 - currentSize);
}

return array;
}

public static void main(String[] args) {
Integer[] arr = {
10,
9,
8,
7,
6,
15,
14,
7,
4,
3,
8,
6,
3,
1,
2,
-2,
-5,
-8,
-3,
-1,
13,
12,
11,
5,
4,
3,
2,
1,
};
PancakeSort pancakeSort = new PancakeSort();
System.out.println("After sorting:");
pancakeSort.sort(arr);
SortUtils.print(arr);
/**
* Finds the index of the maximum element in the array up to the given size.
*
* @param array the array to be searched
* @param currentSize the current size of the unsorted portion of the array
* @param <T> the type of elements in the array
* @return the index of the maximum element
*/
private <T extends Comparable<T>> int findMaxIndex(T[] array, int currentSize) {
T max = array[0];
int maxIndex = 0;
for (int i = 0; i < array.length - currentSize; i++) {
if (SortUtils.less(max, array[i])) {
max = array[i];
maxIndex = i;
}
}
return maxIndex;
}
}
80 changes: 4 additions & 76 deletions src/test/java/com/thealgorithms/sorts/PancakeSortTest.java
Original file line number Diff line number Diff line change
@@ -1,80 +1,8 @@
package com.thealgorithms.sorts;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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);
public class PancakeSortTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new PancakeSort();
}
}