-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp4.cpp
More file actions
48 lines (41 loc) · 844 Bytes
/
Copy pathp4.cpp
File metadata and controls
48 lines (41 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<vector>
using namespace std;
// sort [begin,end)
template<typename T>
void HeapSort(T begin,T end){
int n=end-begin;
auto heapify=[&](auto&& self,int i)->void{
auto& cur=*(begin+i);
int pl=i*2+1,pr=i*2+2;
if(pr<n){
auto &l=*(begin+pl),&r=*(begin+pr);
if(max(l,r)<=cur) return;
if(l>r){
swap(l,cur);
self(self,pl);
}
else{
swap(r,cur);
self(self,pr);
}
}
else if(pl<n && *(begin+pl)>cur)
swap(*(begin+pl),cur);
};
for(int i=n/2-1;i>=0;i--) heapify(heapify,i);
while(--n){
swap(*begin,*(begin+n));
heapify(heapify,0);
}
}
int main(){
int n; cin>>n;
vector<int>a(n);
for(auto &x:a) cin>>x;
HeapSort(a.begin(),a.end());
cout<<"HeapSort: ";
for(auto x:a) cout<<x<<" ";
cout<<endl;
return 0;
}