Skip to content

Commit 9a1d26b

Browse files
authored
Merge pull request algorithm-visualizer#216 from AdhityaRamadhanus/pancake-sort
fix flip optimization in pancake sort function
2 parents c661604 + e92ccf7 commit 9a1d26b

File tree

1 file changed

+1
-1
lines changed
  • algorithm/sorting/pancake/basic

1 file changed

+1
-1
lines changed

algorithm/sorting/pancake/basic/code.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ for (var i=0;i<N-1;i++) {
2121
var currMax = currArr.reduce((prev, curr, idx) => {
2222
return (curr > prev.val) ? { idx: idx, val: curr} : prev;
2323
}, {idx: 0, val: currArr[0]});
24-
if (currMax.idx !== i) {
24+
if (currMax.idx !== 0) { // if currMax.idx === 0 that means max element already at the bottom, no flip required
2525
logger._print('flip at ' + (currMax.idx+i) + ' (step 1)');
2626
flip(currMax.idx+i, N);
2727
logger._print('flip at ' + (i) + ' (step 2)');

0 commit comments

Comments
 (0)