-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB Q-12.cpp
More file actions
47 lines (46 loc) · 766 Bytes
/
Copy pathLAB Q-12.cpp
File metadata and controls
47 lines (46 loc) · 766 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
#include<iostream>
using namespace std;
void max_heapify(int a[],int i,int n)
{
int l=2*i+1;
int r=2*i+2;
int largest;
if(l<n&&a[l]>a[i])
largest=l;
else
largest=i;
if(r<n&&a[r]>a[largest])
largest=r;
if(largest!=i)
{
swap(a[i],a[largest]);
max_heapify(a,largest,n);
}
}
void build(int a[],int n)
{
for(int i=n/2-1;i>=0;i--)
max_heapify(a,i,n);
}
void heapsort(int a[],int n)
{
int s=n;
build(a,n);
for(int i=n-1;i>=0;i--)
{
swap(a[0],a[i]);
s=s-1;
max_heapify(a,0,s);
}
}
int main()
{
int n,i;
int a[20];
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
heapsort(a,n);
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}