forked from Apurvgeek/programs-in-cpp-and-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapsort.cpp
More file actions
43 lines (38 loc) · 718 Bytes
/
heapsort.cpp
File metadata and controls
43 lines (38 loc) · 718 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
#include<bits/stdc++.h>
using namespace std;
void heapifymax(int *a, int n, int root)
{
int key = root, l = 2*root + 1, r = 2*root + 2;
if(l<n && a[l]>a[key]) key = l;
if(r<n && a[r]>a[key]) key = r;
if(key != root)
{
swap(a[root], a[key]);
heapifymax(a, n, key);
}
}
void heapsort(int *a, int n)
{
for(int i = (n-2)/2; i>=0; i--)
{
heapifymax(a, n, i);
}
for(int i = n-1; i>=0; i--)
{
swap(a[i], a[0]);
heapifymax(a, i, 0);
}
}
void printheap(int *a, int n)
{
for(int i = 0; i<n; i++) cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
int n; cin>>n;
int a[n];
for(int i = 0; i<n; i++) cin>>a[i];
heapsort(a, n);
printheap(a, n);
}