归并排序


let mergeSort = (arr,left,right) => {
  if(left < right) {
  let mid = parseInt((left+right)/2);
  mergeSort(arr,left,mid);
  mergeSort(arr,mid+1,right);
  merge(arr,left,mid,right);
  }
}

let merge = (arr,left,mid,right) => {
  let res = [];
  let i = left, j=mid+1;
  let index = 0;
  while(i<=mid&&j<=right){
    res[index++] = arr[i]>arr[j] ? arr[j++] : arr[i++];
  }
  while(i <= mid)  res[index++] = arr[i++];
  while(j <= right)  res[index++] = arr[j++];
  for(let k=0;k

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