Skip to content

Commit 4c1023d

Browse files
Create Maxheapify.cpp
1 parent e4d34b5 commit 4c1023d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Maxheapify.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
6+
void heapify(int arr[], int n, int i)
7+
{
8+
int largest = i;
9+
int l = 2 * i + 1;
10+
int r = 2 * i + 2;
11+
12+
13+
if (l < n && arr[l] > arr[largest])
14+
largest = l;
15+
16+
17+
if (r < n && arr[r] > arr[largest])
18+
largest = r;
19+
20+
21+
if (largest != i) {
22+
swap(arr[i], arr[largest]);
23+
24+
25+
heapify(arr, n, largest);
26+
}
27+
}

0 commit comments

Comments
 (0)