File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments