1 #include<iostream>
2 using namespace std;
3 int n;
4 int a[100];
5
6 void update(int x,int m)//下沉
7 {
8 int l=x*2;
9 if(l>m)return;
10 if(l<m&&a[l]<a[l+1])l++;
11 if(a[l]>a[x])swap(a[l],a[x]);//维护大小根堆
12 update(l,m);
13 }
14 void heap_sort()//排序
15 {
16 for(int i=n;i>=1;i--)update(i,n);
17 for(int i=n-1;i>=1;i--)
18 {
19 swap(a[1],a[i+1]);//每次将堆顶放入最后一个位置(可以用来维护排序大小)
20 update(1,i);
21 }
22 }
23 int main()
24 {
25 cin>>n;
26 for(int i=1;i<=n;i++)cin>>a[i];
27 heap_sort();
28 for(int i=1;i<=n;i++)cout<<a[i]<<" ";
29 }
30 /*
31 #include<iostream>
32 #include<algorithm>
33 using namespace std;
34 int a[100];
35 int n;
36
37 void update(int x,int m)
38 {
39 int t=x*2;
40 if(t>m)return;
41 if(t<m&&a[t+1]>a[t])t++;
42 if(a[t]>a[x])swap(a[t],a[x]);
43 update(t,m);
44 }
45 void heap_sort()
46 {
47 for(int i=n;i>=1;i--)update(i,n);
48 for(int i=n-1;i>=1;i--)
49 {
50 swap(a[i+1],a[1]);
51 update(1,i);
52 }
53 }
54 int main()
55 {
56 cin>>n;
57 for(int i=1;i<=n;i++)cin>>a[i];
58 heap_sort();
59 for(int i=1;i<=n;i++)cout<<a[i]<<" ";
60 }
61
62 */