Write a program for sorting a heap into max heap in C language ..

#include <stdio.h>
#include <conio.h>
int main()
{
int HeapSize, i, j, c, root, temp;

printf(“Enter The Size of the Heap :”);
scanf(“%d”, &HeapSize);
int A[HeapSize];
printf(“Enter the Elements in Heap :\n “);
for (i = 0; i < HeapSize; i++)
{
printf(“Enter the Element At Node no %d : “, i+1);
scanf(“%d”, &A[i]);
}
for (i = 1; i < HeapSize; i++)
{
c = i;
do
{
root = (c – 1) / 2;
if (A[root] < A[c])
{
temp = A[root];
A[root] = A[c];
A[c] = temp;
}
c = root;
} while (c != 0);
}
printf(“The Heap changed into max heap is: “);
for (i = 0; i < HeapSize; i++)
{
printf(“%d\t “, A[i]);
}
}

Input Sample:
Enter The Size of the Heap :7
Enter the Elements in Heap:
Enter the Elements At Node no 1 : 5
Enter the Elements At Node no 2 : 6
Enter the Elements At Node no 3 : 7
Enter the Elements At Node no 4 : 2
Enter the Elements At Node no 5 : 3
Enter the Elements At Node no 6 : 4
Enter the Elements At Node no 7 : 0
The Heap Changed into max heap is: 7 5 6 2 3 4 0

In this output:
The root is 7
Left child of 7 is 5
Right child of 7 is 6
now for 5
Left child of 7 is 2
Right child of 7 is 3
now for 6
Left child of 7 is 4
Right child of 7 is 0

Explanation of this program

This program will sort your tree for max heap .
First of all it will take input(The size of the Heap) then according to your given size
this program will take input the elements.
in 2nd for loop first of all (c=i) indicating that we store the value of i in c for use in do while loop.
this loop will break when the value of c will be 0.
as we know that first element of the heap is called the root so if the value of c will be 0 then means
that the the element at location 0 is root … if(A[root]< A[c]) comparing the parent and child if the
child will be greater the value will be replace by using three statements in the definition of if
condition.
At last we print the all Heap which is sorted…..