void MergeSort(int[] arr) { int l = 0; int r = arr.Length-1; MergeSortSub(arr,l,r); } // 1,2 void MergeSortSub(int[] arr,int left,int right) { if(left == right) return; int mid = left + ((right - left) >> 1); MergeSortSub(arr, left, mid); MergeSortSub(arr, mid+1, right); Merge(arr,left,mid,right); } // right也要传 void Merge(int[] arr,int left,int mid,int right){ int[] help = new int[right-left+1];//* int i=0; int p1 = left; int p2 = mid+1; while(p1 <= mid && p2 <= right) { help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++]; } while(p1 <= mid) { help[i++] = arr[p1++]; } while(p2 <= right) { help[i++] = arr[p2++]; } for ( i = 0; i < help.Length; i++) { arr[left+i] = help[i]; // ** } }
标签:归并,right,c#,arr,mid,int,MergeSortSub,排序,left From: https://www.cnblogs.com/Insist-Y/p/17368717.html