翻烙饼问题简单排序

// 翻转数组指定部分
function reverse(arr, start,end) {
    var len = start + end 
    var temp
    for(var i = start;i <= len/2;i++){
        temp = arr[i]
        arr[i] = arr[len-i]
        arr[len-i] = temp
    }
    return arr
}
function getMaxIndex(arr, start, end){
    var max = arr[start]
    var index = start
    for(var i= start+1;i<=end;i++){
        if (arr[i] > max) {
            max = arr[i]
            index = i
        }
    }
    return index
}
function piesort(arr){
    var len = arr.length
    for (var i = len - 1; i>0;i--){
        var maxIndex = getMaxIndex(arr, 0, i)
        arr = reverse(arr, 0, maxIndex)
        arr = reverse(arr, 0, i)
    }
    return arr
}

console.log(piesort([2,8,6,1,9,3,7,5,4]))