void merge(int arr[],int l,int mid,int r){
   int i = l, j = mid + 1, k = 0;
   int *temp = new int[r- l + 1];
   while(i <= mid && j <= r){
      if(arr[i] <= arr[j]) temp[k++] = arr[i++];
      else temp[k++] = arr[j++];
   }
   while(i <= mid) temp[k++] = arr[i++];
   while(j <= r) temp[k++] = arr[j++];

   for(i = l,k = 0;i <= r; i++,k++){
      arr[i] = temp[k];
   }
   delete []temp;
}
void  static mergeSort(int arr[],int l,int r){
   if(l >= r)
      return ;
   int mid = l + (r-l) / 2;
   mergeSort(arr,l,mid);
   mergeSort(arr,mid + 1,r);
   merge(arr,l,mid,r);
}
void mergeSort(int arr[],int n){
   if(arr == NULL || n < 2)
      return ;
   return mergeSort(arr,0,n-1);
}
最后修改:2020 年 06 月 25 日 07 : 25 PM
如果觉得我的文章对你有用,那就打发点咯~