Skip to content

Commit df14445

Browse files
author
Ram swaroop
committed
code improvements
1 parent 5e02c25 commit df14445

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/me/ramswaroop/arrays/sorting/PancakeSort.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ public static void sort(int[] a) {
3333
maxIndex = j;
3434
}
3535
}
36-
reverse(a, maxIndex); // bring the max element to the front
37-
reverse(a, unsortedIndex); // move the max element to its appropriate index
36+
// move the max element to its appropriate index if its not already in its correct index
37+
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+
}
3841
unsortedIndex--;
3942
}
4043
}

0 commit comments

Comments
 (0)