我有一个c++函数,参数是int* NumList ,请使用Merge Sort算法排序
时间: 2023-05-21 17:04:36 浏览: 78
好的,下面是使用Merge Sort算法排序的C函数代码:
```
void MergeSort(int* NumList, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
MergeSort(NumList, left, mid);
MergeSort(NumList, mid + 1, right);
Merge(NumList, left, mid, right);
}
}
void Merge(int* NumList, int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = NumList[left + i];
for (j = 0; j < n2; j++)
R[j] = NumList[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
NumList[k] = L[i];
i++;
} else {
NumList[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
NumList[k] = L[i];
i++;
k++;
}
while (j < n2) {
NumList[k] = R[j];
j++;
k++;
}
}
```
阅读全文