堆排序,归并排序
include[HTML_REMOVED]
using namespace std;
const int N = 100010;
int q[N],sz,n,w[N];
void down(int u){
int t = u;
if(u*2 <= sz && q[u*2]>q[t]) t = u*2;
if(u*2 + 1<= sz && q[u*2+1]>q[t]) t=u*2+1;
if(t !=u){
swap(q[t],q[u]);
down(t);
}
}
void heap_sort(){
sz = n;
for(int i = n/2;i;i--) down(i);
for(int i =0 ; i<n-1;i++){
swap(q[1],q[sz]);
sz--;
down(1);
}
}
void merge_sort(int l,int r){
if(l>=r) return;
int mid = (l+r) >>1;
merge_sort(l,mid);
merge_sort(mid+1,r);
int i = l,j = mid +1,k=0;
while(i<=mid&&j<=r){
if(q[i]<=q[j]) w[k++] = q[i++];
else w[k++] = q[j++];
}
while(i<=mid) w[k++] = q[i++];
while(j<=r) w[k++] = q[j++];
for(int i=l,j=0;j<k;i++,j++) q[i]=w[j];
}
int main(){
cin>>n;
for(int i = 0;i<n;i++) cin>>q[i];
// heap_sort();
// for(int i = 1;i<=n;i++) cout<<q[i]<<" ";
merge_sort(0,n-1);
for(int i = 0;i<n;i++) cout<<q[i]<<" ";
return 0;
}