我急了😡我急了😡我急了😡我急了😡
2024-01-25 20:30:59
发布于:广东
13阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
using namespace std;
int a[10000005], arr[10000005];
int n;
bool cmp(int a, int b){
return a < b;
}
void merge(int *a, int left, int right){
int mid = (left + right) / 2;
int i = left, j = mid + 1, st = left - 1;
while(i <= mid && j <= right){
if(cmp(a[i], a[j])) arr[++st] = a[i++];
else arr[++st] = a[j++];
}
while(i <= mid) arr[++st] = a[i++];
while(j <= right) arr[++st] = a[j++];
for(int i = left; i <= right; i++){
a[i] = arr[i];
}
}
void mergesort(int *a, int left, int right){
if(left >= right) return;
int mid = (left + right) / 2;
mergesort(a, left, mid);
mergesort(a, mid + 1, right);
merge(a, left, right);
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
mergesort(a, 1, n);
for(int i = 1; i <= n; i++){
cout << a[i] << ' ';
}cout << endl;
return 0;
}
这里空空如也
有帮助,赞一个