U433004
是自己整的一道题目,输入n 和n个数,输出升序排序结果。用堆排序。
然后就错了
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n;
template<class _T>
struct MyHeap{
_T* vec;
int leng;
MyHeap(int _x=0){
leng=_x;
vec=new _T[leng];
}
void Ify(int _x,int _y){//调整
//vec 数组 _x 下标的最大值 _y 需要调整的下标
while(_y*2<=_x){
int _z=_y*2;
if(_z<_x&&vec[_z+1]>vec[_z])
++_z;
if(vec[_z]>vec[_y]){
swap(vec[_z],vec[_y]);
_y=_z;
}
else
return ;
}
return ;
}
void Build(int _x){
//vec 数组 _x 下标的最大值
for(int i=n/2;i>=1;--i)
Ify(_x,i);
return ;
}
void Sort(){
//vec 数组 _x 下标的最大值
Build(leng);
for(int i=leng;i>=1;--i){
swap(vec[1],vec[i]);
Ify(i-1,1);
}
return ;
}
void Reset(int _x){
vec=new _T[_x];
leng=_x;
return ;
}
};
int main(){
MyHeap<int>a;
scanf("%d",&n);
a.Reset(n);
for(int i=1;i<=n;++i)
cin>>a.vec[i];
//scanf("%d",&(a.vec[i]));
a.Sort();
for(int i=1;i<=n;++i)
printf("%d ",a.vec[i]);
return 0;
}