Skip to content

Instantly share code, notes, and snippets.

@jerolan
Created February 15, 2022 22:41
Show Gist options
  • Save jerolan/da8d97a82d5dfeff8127de6e20c01695 to your computer and use it in GitHub Desktop.
Save jerolan/da8d97a82d5dfeff8127de6e20c01695 to your computer and use it in GitHub Desktop.
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