We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5e02c25 commit df14445Copy full SHA for df14445
src/me/ramswaroop/arrays/sorting/PancakeSort.java
@@ -33,8 +33,11 @@ public static void sort(int[] a) {
33
maxIndex = j;
34
}
35
36
- reverse(a, maxIndex); // bring the max element to the front
37
- reverse(a, unsortedIndex); // move the max element to its appropriate index
+ // move the max element to its appropriate index if its not already in its correct index
+ if (maxIndex != unsortedIndex) {
38
+ reverse(a, maxIndex); // bring the max element to the front
39
+ reverse(a, unsortedIndex); // move the max element to its appropriate index
40
+ }
41
unsortedIndex--;
42
43
0 commit comments