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 43 44
|
static void merge_sort(int arr[], int temp[], int left, int right) { if (left >= right) return;
int mid = (left + right) / 2;
merge_sort(arr, temp, left, mid); merge_sort(arr, temp, mid + 1, right);
int k = 0, i = left, j = mid + 1;
while (i <= mid && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } } while (i <= mid) { temp[k++] = arr[i++]; } while (j <= right) { temp[k++] = arr[j++]; }
for (int ai = left, ti = 0; ai <= right; ai++, ti++) { arr[ai] = temp[ti]; } }
|