-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSortASC.cpp
More file actions
63 lines (57 loc) · 1.24 KB
/
HeapSortASC.cpp
File metadata and controls
63 lines (57 loc) · 1.24 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
class MaxHeap{
public:
void Max_Hapify(int A[],int i,int n)
{
int l,r;
int largest = i;
l = (2*i);
r = (2*i)+1;
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_Hapify(A,largest,n);
}
}
void Build_MaxHeap(int A[],int n)
{
for(int i=(n/2);i>=1;i--)
Max_Hapify(A,i,n);
for(int i = n; i > 1; i--)
{
swap(A[1],A[i]);
Max_Hapify(A,1,i-1);
}
}
void PrintHeap(int A[],int n)
{
cout<<"Array Representation of Heap is : "<<endl;
for(int i=1;i<=n;i++)
cout<<A[i]<<" ";
cout<<endl;
}
};
int main()
{
freopen("in.txt","r",stdin);
MaxHeap H;
int A[100];
int n;
cout<<"Enter the Element No : "<<endl;
cin>>n;
cout<<"Enter the Elements "<<endl;
for(int i=1;i<=n;i++)
{
cin>>A[i];
}
H.Build_MaxHeap(A,n);
H.PrintHeap(A,n);
return 0;
}