-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaxHeapify.cpp
69 lines (57 loc) · 1.49 KB
/
MaxHeapify.cpp
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
// Given a unosorted array , convert it into a maxHeap from it
#include<iostream>
using namespace std;
void maxHeapify(int arr[],int n,int i){
int curr_idx = i;
int leftIndex = 2*i + 1 ;
int rightIndex = 2*i + 2 ;
if(leftIndex < n && arr[leftIndex] > arr[curr_idx]){
curr_idx = leftIndex;
}
if(rightIndex < n && arr[rightIndex] > arr[curr_idx]){
curr_idx = rightIndex;
}
if(curr_idx!=i){
// curr_index got updated ,
// now the current index reached to correct place where the element at index i has to move
// Swap them
swap(arr[i],arr[curr_idx]);
// Recursively heapify the affected sub-tree
maxHeapify(arr,n,curr_idx);
}
}
void insert(int arr[],int n,int val){
// add the element at the end of array
arr[n] = val ;
n++;
// Now move it to correct place using bottom to top approach
int i = n - 1;
while(i>0){
int parentIndex = (i-1)/2;
if(arr[parentIndex]<arr[i]){
swap(arr[i],arr[parentIndex]);
i = parentIndex;
}
else{
return ;
}
}
}
void buildMaxHeap(int arr[],int n){
for(int i=n/2-1 ; i>=0 ; i--){
maxHeapify(arr,n,i);
}
}
int main(){
int arr[20] = {10,1,23,50,7,-4};
buildMaxHeap(arr,6);
for(int i=0;i<6;i++){
cout << arr[i] << " " ;
}
cout << endl;
insert(arr,6,210);
for(int i=0;i<7;i++){
cout << arr[i] << " " ;
}
return 0;
}