### Problem Statement:

You are given an array of N elements. The task is to build a Binary Heap from the given array, either Max Heap or Min Heap.

### Quick Facts:

Now, for sorting the elements inside the array, we can use many other sorting methods but we have to sort the array elements using the heap sort, so there are a few things that you need to keep in your mind while sorting which are

- You need to build the heap out of the elements which are provided for sorting unlike other sorting methods, and this sorting is known as one of the best sorting algorithms when you compare it with selection, insertion, and other sorting algorithms.
- In heap sort, you donâ€™t have to waste time checking with the unsorted region in linear-time complexity which might go to adding an extra complexity of O(n), here you just need to look from the unsorted region maintained in the form of the heap to find the largest element more quickly as compared to the other sorting algorithms in each step.

### Quick Think:

Now, let us have a discussion before we start sorting the array with heap sort

- Now, in the given array of elements, we will first check, whether the complete binary tree from the array forms the heap or not, if it does not begin then, we will heapify the elements of the array forming a complete binary tree in reverse level order traversal.
- Now, if we analyze the time taken in forming the heap from n array elements would be n times, and then, also heapifying a single node takes a time of o(logn) complexity making it a total of O(n*logn) time complexity.

Now, reading the above Quick Think section we will have to think of some optimized approach to solving this problem which would obviously require less time complexity than above.

When you carefully notice that in the given input array the leaf nodes need not be heapified as they already follow the heap properties also we can see that the binary tree is formed in the level order, traversing the array from start to end to build the tree level by level, so how and on which element we will be performing the heapify operation?

As mentioned in the above paragraph we will find the position of the last non-leaf node and perform the heapify operation of each non-leaf node in reverse level order.

Now, letâ€™s discuss the algorithm for building a heap from an array

### Algorithm:

**Step-1:**Get the index of the first non-leaf node through the relation (n / 2) – 1 and perform level order traversal from the last leaf node and heapify each node**Step-2 (Heap Function):**Now, we will assume the index (while the heapify function was called) we have has the largest data node.**Step-3:**Now, similar to the above step get the index of the left and the right child node**Step-4:**Now, compare that if the left child node is greater than the current largest node then, the largest node would be the current left child**Step-5:**Again, compare that if the right child node is greater than the current largest node then, the largest node would be the current right child**Step-6:**If the current largest node is not the present index node then, we will have to swap the nodes to make it a max heap, then go to step 7**Step-7:**Now, in the previous step we have swapped the two nodes to convert it to the max heap, so, we will again have to re-arrange all the nodes by heapifying the nodes again

### Implementation Of The Above Algorithm:

import java.util.Scanner; class Armstrong { public static void main(String[] arg) { int a,arm=0,n,temp; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextInt(); temp=n; for( ;n!=0;n/=10 ) { a=n%10; arm=arm+(a*a*a); } if(arm==temp) System.out.println(temp+" is a armstrong number "); else System.out.println(temp+" is not a armstrong number "); } }

**The Running Time Complexity Of the Above Program is :** O(nlogn)

**The Output Of The Above Program Is :** 5 6 7 11 12 13

### Explanation Of The Above Code:

This article tried to discuss, Building Heap from Array. Hope this blog helps you understand and solve the problem. To practice more problems you can check out MYCODE | Competitive Programming at PrepBytes.

What is the space complexity for this approach?