1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| func MergeSort(p, q []int, l, r int) { if l == r { return }
mid := (l + r) >> 1 MergeSort(p, q, l, mid) MergeSort(p, q, mid+1, r)
k, i, j := 0, l, mid+1
for i <= mid && j <= r { if q[i] <= q[j] { p[k] = q[i] k++ i++ } else { p[k] = q[j] k++ j++ } } for i <= mid { p[k] = q[i] k++ i++ } for j <= r { p[k] = q[j] k++ j++ }
i, j = l, 0 for i < r { q[i] = p[j] i++ j++ }
}
|