题解 | #排序# 归并排序
排序
https://www.nowcoder.com/practice/2baf799ea0594abd974d37139de27896
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 将给定数组排序 * @param arr int整型一维数组 待排序的数组 * @return int整型一维数组 */ function MySort( arr ) { if(arr.length < 2) return arr; const middle = Math.floor(arr.length / 2); const left = arr.slice(0,middle); const right = arr.slice(middle); return mergeSort(MySort(left),MySort(right)); } function mergeSort(left,right){ const result = []; while(left.length && right.length){ if(left[0] <= right[0]){ result.push(left.shift()); }else{ result.push(right.shift()); } } while(left.length){ result.push(left.shift()); } while(right.length){ result.push(right.shift()); } return result; } module.exports = { MySort : MySort };