Created
February 15, 2022 22:41
-
-
Save jerolan/da8d97a82d5dfeff8127de6e20c01695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flip(arr, right) { | |
let temp; | |
let left = 0; | |
while (left < right) { | |
temp = arr[left]; | |
arr[left] = arr[right]; | |
arr[right] = temp; | |
// scroll | |
left+=1; | |
right-=1; | |
} | |
} | |
function findIndexOfMax(array, limit) { | |
let target = 0; | |
for (let index = 0; index < limit; index+=1) { | |
if (array[index] > array[target]) target = index; | |
} | |
return target; | |
} | |
function pancakeSort(array) { | |
for (let i = array.length; i > 1; --i) { | |
let indexOfMax = findIndexOfMax(array, i); | |
if (indexOfMax != i) { | |
flip(array, indexOfMax); | |
} | |
flip(array, i - 1); | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment