-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSortDSC.cpp
More file actions
64 lines (57 loc) · 1.25 KB
/
HeapSortDSC.cpp
File metadata and controls
64 lines (57 loc) · 1.25 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
64
#include<bits/stdc++.h>
using namespace std;
class MinHeap{
public:
void Min_Hapify(int A[],int i,int n)
{
int l,r;
int smallest = i;
l = (2*i);
r = (2*i)+1;
if(l<=n && A[l] < A[i])
smallest = l;
else
smallest = i;
if(r<=n && A[r] < A[smallest])
smallest = r;
if(smallest!=i)
{
swap(A[i],A[smallest]);
Min_Hapify(A,smallest,n);
}
}
void Build_MinHeap(int A[],int n)
{
for(int i=(n/2); i >= 1; i--)
Min_Hapify(A,i,n);
for(int i = n; i > 1; i--)
{
swap(A[1],A[i]);
Min_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);
MinHeap M;
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];
}
M.Build_MinHeap(A,n);
M.PrintHeap(A,n);
return 0;
}