Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Max Heap Added. #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions data_structures/Heap/Build Max Heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<bits/stdc++.h>

using namespace std;

//5,3,2,25,7,17,20,28,4
void maxHeapify(int a[],int n,int index){
int l,r,largest;
l=(index+1)*2-1;
r=(index+1)*2;

if(l<n && a[l]>a[index]){
largest=l;
}
else{
largest=index;
}

if(r<n && a[r]>a[largest]){
largest=r;
}

if(largest!=index){
int temp=a[index];
a[index]=a[largest];
a[largest]=temp;

maxHeapify(a,n,largest);
}
}

void buildHeap(int a[],int n){
for(int i=n/2;i>=0;i--){
maxHeapify(a,n,i);
}
}

int main(){
int a[]={65,3,22,25,27,17,20,8,4};

int n=sizeof(a)/sizeof(int);
buildHeap(a,n);

for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}