# Build a binary-heap from an array, which method is more efficient and why

I am learning about heaps, and I have found two ways of building them from a given array: I am trying to build up a MAX Heap.

### 1. Top-down approach

Here I check for every element to see if it is at the correct position or not. By using a function called restoreUp, in which every key is compared to its parent key, and if the parent key is smaller than the parent key is moved down. This procedure continues until the parent key is greater. I check it for every key starting at index position 2.

My code is:

void restoreUp(int arr[],int i)
{
int k=arr[i];
int par=i/2;
while(arr[par]<k)
{
arr[i]=arr[par];
i=par;
par=i/2;
}
arr[i]=k;
}
void buildheap1(int arr[],int size)
{
int i;
for(i=2;i<=size;i++)
restoreUp(arr,i);
}


### 2. Bottom-up approach

Here I start from the first non-leaf node present at index floor(size/2), and call a function restoreDown until node number 1. I compare a key with both its left and right child and then the greater child is moved up. If both children are greater than the key, then I move the larger of the two children up. This procedure stops when both children are smaller than the key.

My code is:

void restoreDown(int arr[],int i,int size)
{
int left=2*i;
int right=2*i+1;
int num=arr[i];
while(right<=size)
{
if(num>arr[left] && num>arr[right])
{
arr[i]=num;
return;
}
else if(arr[left]>arr[right])
{
arr[i]=arr[left];
i=left;
}
else
{
arr[i]=arr[right];
i=right;
}
left=2*i;
right=2*i+1;
}
if(left==size && arr[left]>num)
{
arr[i]=arr[left];
i=left;
}
arr[i]=num;
}
void buildheap2(int arr[],int size)
{
int i;
for(i=size/2;i>=1;i--)
restoreDown(arr,i,size);
}


Both the methods are working for me. Which method is more efficient and why?