归并排序(一种对称美观的写法)
归并排序(一种对称美观的写法)
void msort(int* arr, int *buf,int L, int M, int R) {
if (L < M - 1)msort(arr, buf, L, L + (M - L >> 1),M);
if (M < R - 1)msort(arr, buf, M, M + (R - M >> 1), R);
if (L < R - 1) {
int p1 = L, p2 = M, pbuf = L;
while (p1 < M && p2 < R) {
if (arr[p1] > arr[p2])
buf[pbuf++] = arr[p2++];
else
buf[pbuf++] = arr[p1++];
}
while (p1 < M)buf[pbuf++] = arr[p1++];
while (p2 < R)buf[pbuf++] = arr[p2++];
while (--pbuf >= L)arr[pbuf] = buf[pbuf];
}
}