-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
77 lines (64 loc) · 1.03 KB
/
heap.cpp
File metadata and controls
77 lines (64 loc) · 1.03 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
65
66
67
68
69
70
71
72
73
74
75
76
// NAME ABHISHEK KUMAR LABH
// ROLL NO: 09/CSE/52
#include<iostream>
using namespace std;
class heap{
public:
int *h;
heap(int n)
{
h=new int[n];
cout<<"enter the no to be sorted\n";
for(int a=0;a<n;a++)
{
cout<<"h["<<a<<"]=";
cin>>h[a];
}
}
void Heap(int n) //heap the element
{
for(int i=(n/2)-1;i>=0;i--)
{
if(h[i]<h[2*i+1])
{
swap(i,i*2+1);
}
if(2*i+2!=n || n%2!=0)
{
if(h[i]<h[i*2+2])
{
swap(i,2*i+2);
}
}
}
swap(0,n-1);
if(n!=1){
Heap(n-1);
}
}
void swap(int x,int y)
{
int item=h[x];
h[x]=h[y];
h[y]=item;
}
void display(int n)
{
cout<<"sorted no are"<<endl;
for(int i=0;i<n;i++)
{
cout<<"h["<<i<<"]="<<h[i]<<endl;
}
}
};
int main()
{
int m;
cout<<"enter the no of element:";
cin>>m;
heap h1(m);
//h1.n=m;
h1.Heap(m);
h1.display(m);
return 0;
}