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

Lines changed: 1 addition & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy