歸并模板
2021-09-15 13:31 作者:54335443735_bili | 我要投稿
public static void mergeSort(int[] a, int l , int r){
? ?if (l >= r) return;
? ?int m = l + r >> 1;
? ?mergeSort(a, l , m);
? ?mergeSort(a, m + 1, r);
? ?int k = 0, i = l , j = m + 1;
? ?int[] tmp = new int[r - l + 1];
? ?while (i<= m && j <= r){
? ? ? ?if (a[i] < a[j]) tmp[k ++ ] = a[i ++];
? ? ? ?else tmp[k++] = a[j ++];
? ?}
? ?while (i <= m) tmp[k ++] = a[i++];
? ?while (j <= r) tmp[k++] = a[j++];
? ?for (i = l, j = 0; i <= r; i++, j++)a[i ] = tmp[j];
}
標簽: