Skip to content

Commit c042a56

Browse files
authored
Merge branch 'master' into feat/patience_sort
2 parents d63b574 + 08db744 commit c042a56

File tree

2 files changed

+29
-122
lines changed

2 files changed

+29
-122
lines changed

src/main/java/com/thealgorithms/sorts/PancakeSort.java

+25-46
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,35 @@ public class PancakeSort implements SortAlgorithm {
1010

1111
@Override
1212
public <T extends Comparable<T>> T[] sort(T[] array) {
13-
int size = array.length;
13+
if (array.length < 2) {
14+
return array;
15+
}
1416

15-
for (int i = 0; i < size; i++) {
16-
T max = array[0];
17-
int index = 0;
18-
for (int j = 0; j < size - i; j++) {
19-
if (SortUtils.less(max, array[j])) {
20-
max = array[j];
21-
index = j;
22-
}
23-
}
24-
SortUtils.flip(array, index, array.length - 1 - i);
17+
for (int currentSize = 0; currentSize < array.length; currentSize++) {
18+
int maxIndex = findMaxIndex(array, currentSize);
19+
SortUtils.flip(array, maxIndex, array.length - 1 - currentSize);
2520
}
21+
2622
return array;
2723
}
2824

29-
public static void main(String[] args) {
30-
Integer[] arr = {
31-
10,
32-
9,
33-
8,
34-
7,
35-
6,
36-
15,
37-
14,
38-
7,
39-
4,
40-
3,
41-
8,
42-
6,
43-
3,
44-
1,
45-
2,
46-
-2,
47-
-5,
48-
-8,
49-
-3,
50-
-1,
51-
13,
52-
12,
53-
11,
54-
5,
55-
4,
56-
3,
57-
2,
58-
1,
59-
};
60-
PancakeSort pancakeSort = new PancakeSort();
61-
System.out.println("After sorting:");
62-
pancakeSort.sort(arr);
63-
SortUtils.print(arr);
25+
/**
26+
* Finds the index of the maximum element in the array up to the given size.
27+
*
28+
* @param array the array to be searched
29+
* @param currentSize the current size of the unsorted portion of the array
30+
* @param <T> the type of elements in the array
31+
* @return the index of the maximum element
32+
*/
33+
private <T extends Comparable<T>> int findMaxIndex(T[] array, int currentSize) {
34+
T max = array[0];
35+
int maxIndex = 0;
36+
for (int i = 0; i < array.length - currentSize; i++) {
37+
if (SortUtils.less(max, array[i])) {
38+
max = array[i];
39+
maxIndex = i;
40+
}
41+
}
42+
return maxIndex;
6443
}
6544
}
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,8 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
5-
import org.junit.jupiter.api.DisplayName;
6-
import org.junit.jupiter.api.Test;
7-
8-
public class PancakeSortTest {
9-
10-
private PancakeSort pancakeSort = new PancakeSort();
11-
12-
@Test
13-
@DisplayName("Empty Array pancakeSort")
14-
public void pancakeSortEmptyArray() {
15-
Integer[] inputArray = {};
16-
Integer[] outputArray = pancakeSort.sort(inputArray);
17-
assertThat(outputArray).isEmpty();
18-
}
19-
20-
@Test
21-
@DisplayName("PancakeSort single Integer Array")
22-
public void pancakeSort() {
23-
Integer[] inputArray = {2};
24-
Integer[] outputArray = pancakeSort.sort(inputArray);
25-
assertThat(outputArray).isEqualTo(inputArray);
26-
}
27-
28-
@Test
29-
@DisplayName("PancakeSort non duplicate Integer Array")
30-
public void pancakeSortNonDuplicateIntegerArray() {
31-
Integer[] inputArray = {2, 1, 77, 34, 14, 56, 8};
32-
Integer[] expectedOutput = {1, 2, 8, 14, 34, 56, 77};
33-
Integer[] outputArray = pancakeSort.sort(inputArray);
34-
assertThat(outputArray).isEqualTo(expectedOutput);
35-
}
36-
37-
@Test
38-
@DisplayName("PancakeSort Integer Array with duplicates")
39-
public void pancakeSortDuplicateIntegerArray() {
40-
Integer[] inputArray = {2, 1, 77, 34, 14, 77, 56, 14, 8};
41-
Integer[] expectedOutput = {1, 2, 8, 14, 14, 34, 56, 77, 77};
42-
Integer[] outputArray = pancakeSort.sort(inputArray);
43-
assertThat(outputArray).isEqualTo(expectedOutput);
44-
}
45-
46-
@Test
47-
@DisplayName("PancakeSort negative Integer Array with duplicates")
48-
public void pancakeSortNegativeDuplicateIntegerArray() {
49-
Integer[] inputArray = {2, 1, 77, -34, -14, 77, 56, -14, 8};
50-
Integer[] expectedOutput = {-34, -14, -14, 1, 2, 8, 56, 77, 77};
51-
Integer[] outputArray = pancakeSort.sort(inputArray);
52-
assertThat(outputArray).isEqualTo(expectedOutput);
53-
}
54-
55-
@Test
56-
@DisplayName("PancakeSort single String Array")
57-
public void pancakeSortSingleStringArray() {
58-
String[] inputArray = {"W"};
59-
String[] outputArray = pancakeSort.sort(inputArray);
60-
assertThat(outputArray).isEqualTo(inputArray);
61-
}
62-
63-
@Test
64-
@DisplayName("PancakeSort non duplicate String Array")
65-
public void pancakeSortNonDuplicateStringArray() {
66-
String[] inputArray = {"W", "A", "d", "be", "jk", "hb", "bgh"};
67-
String[] expectedOutput = {"A", "W", "be", "bgh", "d", "hb", "jk"};
68-
String[] outputArray = pancakeSort.sort(inputArray);
69-
assertThat(outputArray).isEqualTo(expectedOutput);
70-
}
71-
72-
@Test
73-
@DisplayName("PancakeSort String Array with duplicates")
74-
public void pancakeSortDuplicateStringArray() {
75-
String[] inputArray = {"W", "A", "d", "be", "jk", "hb", "bgh", "bgh", "W"};
76-
String[] expectedOutput = {"A", "W", "W", "be", "bgh", "bgh", "d", "hb", "jk"};
77-
String[] outputArray = pancakeSort.sort(inputArray);
78-
assertThat(outputArray).isEqualTo(expectedOutput);
3+
public class PancakeSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new PancakeSort();
797
}
808
}

0 commit comments

Comments
 (0)