8000 Merge pull request #1 from oo7dotcom/oo7dotcom-patch-1 · oo7dotcom/Algorithms@3fe1116 · GitHub
[go: up one dir, main page]

Skip 8000 to content

Commit 3fe1116

Browse files
authored
Merge pull request #1 from oo7dotcom/oo7dotcom-patch-1
Create Max Heap
2 parents 65e33f7 + 7e3e089 commit 3fe1116

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Max Heap

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
using namespace std;
3+
void max_heap(int *a, int m, int n) {
4+
int j, t;
5+
t = a[m];
6+
j = 2 * m;
7+
while (j <= n) {
8+
if (j < n && a[j+1] > a[j])
9+
j = j + 1;
10+
if (t > a[j])
11+
break;
12+
else if (t <= a[j]) {
13+
a[j / 2] = a[j];
14+
j = 2 * j;
15+
}
16+
}
17+
a[j/2] = t;
18+
return;
19+
}
20+
void build_maxheap(int *a,int n) {
21+
int k;
22+
for(k = n/2; k >= 1; k--) {
23+
max_heap(a,k,n);
24+
}
25+
}
26+
int main() {
27+
int n, i;
28+
cout<<"enter no of elements of array\n";
29+
cin>>n;
30+ int a[30];
31+
for (i = 1; i <= n; i++) {
32+
cout<<"enter elements"<<" "<<(i)<<endl;
33+
cin>>a[i];
34+
}
35+
build_maxheap(a,n);
36+
cout<<"Max Heap\n";
37+
for (i = 1; i <= n; i++) {
38+
cout<<a[i]<<endl;
39+
}
40+
}

0 commit comments

Comments
 (0)
0