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 42
|
void mergeSort(int[] arr) { mergeSort(arr, 0, arr.length - 1); }
void mergeSort(int[] arr, int low, int height) { if (low >= height) { return; } int mid = (low + height) / 2; mergeSort(arr, low, mid); mergeSort(arr, mid + 1, height); merge(arr, low, mid, height); }
void merge(int[] arr, int low, int mid, int height) { int[] tempArr = Arrays.copyOf(arr, arr.length); int index = low; int i = low, j = mid + 1; while (i <= mid && j <= height) { if (tempArr[i] <= tempArr[j]) { arr[index] = tempArr[i]; i++; } else { arr[index] = tempArr[j]; j++; } index++; } while (i <= mid) { arr[index] = tempArr[i]; i++; index++; } while (j <= height) { arr[index] = tempArr[j]; j++; index++; } }
|