Using Bottom-Up Heapify – O(n) Time and O(log n) Space
The idea is to heapify operations from the last internal node up to the root. Since leaf nodes already satisfy heap property, we only need to fix internal nodes. By heapifying in bottom-up order, every subtree becomes a valid Max Heap, resulting in the complete array forming a Max Heap efficiently.
Start from the last non-leaf node of the heap
Apply MaxHeapify on each internal node while moving towards the root
During heapify, compare node with its children and swap with the larger child if needed
Repeat recursively until Max Heap property is satisfied for all nodes
C++
#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;// to heapify a subtree with root at given indexvoidmaxHeapify(vector<int>&arr,inti){intn=arr.size();intl=2*i+1;intr=2*i+2;intlargest=i;if(l<n&&arr[l]>arr[i])largest=l;if(r<n&&arr[r]>arr[largest])largest=r;if(largest!=i){swap(arr[i],arr[largest]);maxHeapify(arr,largest);}}// This function basically builds max heapvoidconvertMaxHeap(vector<int>&arr){intn=arr.size();// Start from bottommost and rightmost// internal node and heapify all internal// nodes in bottom up wayfor(inti=(n-2)/2;i>=0;--i)maxHeapify(arr,i);}// Driver's codeintmain(){// array representing Min Heapvector<int>arr={3,5,9,6,8,20,10,12,18,9};convertMaxHeap(arr);for(inti=0;i<arr.size();++i)cout<<arr[i]<<" ";return0;}
Java
importjava.util.*;classGFG{// to heapify a subtree with root at given indexstaticvoidmaxHeapify(int[]arr,inti){intn=arr.length;intl=2*i+1;intr=2*i+2;intlargest=i;if(l<n&&arr[l]>arr[i])largest=l;if(r<n&&arr[r]>arr[largest])largest=r;if(largest!=i){inttemp=arr[i];arr[i]=arr[largest];arr[largest]=temp;maxHeapify(arr,largest);}}// This function basically builds max heapstaticvoidconvertMaxHeap(int[]arr){intn=arr.length;// Start from bottommost and rightmost// internal node and heapify all internal// nodes in bottom up wayfor(inti=(n-2)/2;i>=0;--i)maxHeapify(arr,i);}// Driver's codepublicstaticvoidmain(String[]args){// array representing Min Heapint[]arr={3,5,9,6,8,20,10,12,18,9};convertMaxHeap(arr);for(inti=0;i<arr.length;++i)System.out.print(arr[i]+" ");}}// Contributed by Pramod Kumar
Python
# to heapify a subtree with root at given indexdefmaxHeapify(arr,i):n=len(arr)l=2*i+1r=2*i+2largest=iifl<nandarr[l]>arr[i]:largest=lifr<nandarr[r]>arr[largest]:largest=riflargest!=i:arr[i],arr[largest]=arr[largest],arr[i]maxHeapify(arr,largest)# This function basically builds max heapdefconvertMaxHeap(arr):n=len(arr)# Start from bottommost and rightmost# internal node and heapify all internal# nodes in bottom up wayforiinrange((n-2)//2,-1,-1):maxHeapify(arr,i)# Driver's code# array representing Min Heaparr=[3,5,9,6,8,20,10,12,18,9]convertMaxHeap(arr)foriinrange(len(arr)):print(arr[i],end=" ")
C#
usingSystem;classGFG{// to heapify a subtree with root at given indexstaticvoidmaxHeapify(int[]arr,inti){intn=arr.Length;intl=2*i+1;intr=2*i+2;intlargest=i;if(l<n&&arr[l]>arr[i])largest=l;if(r<n&&arr[r]>arr[largest])largest=r;if(largest!=i){inttemp=arr[i];arr[i]=arr[largest];arr[largest]=temp;maxHeapify(arr,largest);}}// This function basically builds max heapstaticvoidconvertMaxHeap(int[]arr){intn=arr.Length;// Start from bottommost and rightmost// internal node and heapify all internal// nodes in bottom up wayfor(inti=(n-2)/2;i>=0;--i)maxHeapify(arr,i);}// Driver's codestaticvoidMain(){// array representing Min Heapint[]arr={3,5,9,6,8,20,10,12,18,9};convertMaxHeap(arr);for(inti=0;i<arr.Length;++i)Console.Write(arr[i]+" ");}}// This code is contributed by nitin mittal.
JavaScript
// to heapify a subtree with root at given indexfunctionmaxHeapify(arr,i){letn=arr.length;letl=2*i+1;letr=2*i+2;letlargest=i;if(l<n&&arr[l]>arr[i])largest=l;if(r<n&&arr[r]>arr[largest])largest=r;if(largest!=i){[arr[i],arr[largest]]=[arr[largest],arr[i]];maxHeapify(arr,largest);}}// This function basically builds max heapfunctionconvertMaxHeap(arr){letn=arr.length;// Start from bottommost and rightmost// internal node and heapify all internal// nodes in bottom up wayfor(leti=Math.floor((n-2)/2);i>=0;--i)maxHeapify(arr,i);}// Driver's codeletarr=[3,5,9,6,8,20,10,12,18,9];convertMaxHeap(arr);for(leti=0;i<arr.length;++i)process.stdout.write(arr[i]+" ");