private static void mergeSort(int[] arr) { mergeSort(arr, 0, arr.length - 1); }
private static void mergeSort(int[] arr, int l, int r) { if (l == r) return;
int m = l + r >>> 1; mergeSort(arr, l, m); mergeSort(arr, m + 1, r);
merge(arr, l, m, r); }
private static void merge(int[] arr, int l, int m, int r) { int p = l, q = m + 1; int[] tmp = new int[r - l + 1]; int i = 0; while (p <= m && q <= r) tmp[i++] = arr[p] <= arr[q] ? arr[p++] : arr[q++]; while (p <= m) tmp[i++] = arr[p++]; while (q <= r) tmp[i++] = arr[q++];
for (int val : tmp) arr[l++] = val; }
|