归并排序(JavaScript)

function mergeSort(arr){
  return mergeSortRec(arr)
}
function mergeSortRec(arr){
  if(arr.length===1){
    return arr;
  }
  let mid = Math.floor(arr.length/2);
  let left = arr.slice(0,mid);
  let right = arr.slice(mid,arr.length)
  return merge(mergeSortRec(left), mergeSortRec(right))
}
function merge(left,right){
  let result = [],i=0,j=0;
  while(i

你可能感兴趣的:(归并排序(JavaScript))