#include<bits/stdc++.h>
using namespace std;
int n,a[1000],b[1000];
void hebin(int l,int mid,int r){
int p=l, q = mid+1;
for(int i=l;i<=r;i++){
if((q>r)||(p<=mid&&a[p]<=a[q])){
b[i] = a[p++];
}
else b[i] = a[q++];
}
for(int i=l;i<=r;i++)
a[i] = b[i];
}
void merge_sort(int l,int r){
if(l==r) return ;
int mid = (l+r)/2;
merge_sort(l,mid);
merge_sort(mid+1,r);
hebin(l,mid,r);
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
merge_sort(0,n-1);
for(int i=0;i<n;i++){
cout << a[i];
}
}
【C++】归并排序
最新推荐文章于 2024-11-15 21:44:47 发布