-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHeapSort.cpp
38 lines (38 loc) · 1006 Bytes
/
HeapSort.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n,a[100000];
void max_heapify(int a[],int i,int n){ /* Complexity of max_heapify is O(log(n)) */
int left=2*i,right=2*i+1;
int largest;
if(left<=n && a[left]>=a[i])
largest=left;
else largest=i;
if(right<=n && a[right]>=a[largest])
largest=right;
if(largest != i){
swap(a[i],a[largest]);
max_heapify(a,largest,n);
}
}
void build_max_heap(int a[],int n){ /* Complexity of build_max_heap is O(n) */
for(int i=n/2;i>=1;i--)
max_heapify(a,i,n);
}
void heap_sort(int a[],int n){
int heap_size=n;
build_max_heap(a,n);
for(int i=n;i>=2;i--){
swap(a[1],a[i]);
heap_size--;
max_heapify(a,1,heap_size);
}
}
int main(int argc, char const *argv[])
{
ios_base::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
heap_sort(a,n);
for(int i=1;i<=n;i++ ) cout<<a[i]<<" ";
return 0;
}