用C语言根据void MergeSort(int* a, int begin, int end, int* temp)写出归并排序函数
时间: 2024-01-01 16:04:18 浏览: 69
C++实现归并排序(MergeSort)
void MergeSort(int* a, int begin, int end, int* temp) {
if (begin >= end) return;
int mid = (begin + end) / 2;
MergeSort(a, begin, mid, temp);
MergeSort(a, mid + 1, end, temp);
int i = begin, j = mid + 1, k = 0;
while (i <= mid && j <= end) {
if (a[i] <= a[j]) {
temp[k++] = a[i++];
} else {
temp[k++] = a[j++];
}
}
while (i <= mid) temp[k++] = a[i++];
while (j <= end) temp[k++] = a[j++];
for (i = 0; i < k; i++) {
a[begin + i] = temp[i];
}
}
阅读全文