解
2024-04-04 21:02:29
发布于:北京
100阅读
0回复
0点赞
#include<iostream>
using namespace std;
int n;
int a[1005];
int tmp[1005];
void d(int l, int r){
if(l == r)return;
int mid = (l + r) / 2;
d(l, mid);
d(mid + 1, r);
int i = l, j = mid + 1, k = l;
while(i <= mid && j <= r){
if(a[i] <= a[j]) tmp[k ++] = a[i ];
else tmp[k] = a[j ++];
}
while(i <= mid) tmp[k ++] = a[i ++];
while(j <= r) tmp[k ++] = a[j ++];
for(int i = l; i <= r; i ++) a[i] = tmp[i];
for(int i = 1; i <= n; i ++){
cout << a[i] << ' ';
}
cout << endl;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
d(1, n);
return 0;
}
这里空空如也
有帮助,赞一个