#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n;
template<class _T>
struct MyHeap{
vector<_T>vec;
int leng;
MyHeap(int _x=0){
leng=_x;
vec.resize(_x);
}
void Ify(int _x,int _y){//调整
// _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){
// _x 下标的最大值
for(int i=n/2;i>=1;--i)
Ify(_x,i);
return ;
}
void Sort(){
// _x 下标的最大值
Build(leng);
for(int i=leng;i>=1;--i){
swap(vec[1],vec[i]);
Ify(i-1,1);
}
return ;
}
void resize(int _x){
vec.resize(_x);
leng=_x;
return ;
}
};
int main(){
MyHeap<int>a;
scanf("%d",&n);
a.resize(n);
for(int i=1;i<=n;++i)
scanf("%d",&(a.vec[i]));
a.Sort();
for(int i=1;i<=n;++i)
printf("%d ",a.vec[i]);
return 0;
}
就是输入N,n个数,输出N个数排序后的结果(空格隔开)。
然后就错了第一个点。