#include<stdio.h>
#include<stdlib.h>
void MaxHeapify(int a[],int i,int n)
{
int j,temp;
temp=a[i];
j=2*i;
while(j<=n)
{
if(j<n && a[j+1]>a[j])
j=j++;
if(temp>a[j])
break;
else if(temp<=a[j])
{
a[j/2]=a[j];
j=2*j;
}
}
a[j/2]=temp;
return;
}
void MinHeapify(int a[],int i,int n)
{
int j,temp;
temp=a[i];
j=2*i;
while(j<=n)
{
if(j<n && a[j+1]<a[j])
j=j++;
if(temp<a[j])
break;
else if(temp>=a[j])
{
a[j/2]=a[j];
j=2*j;
}
}
a[j/2]=temp;
return;
}
void MaxHeapsort(int a[],int n)
{
int i,temp;
for(i=n;i>=2;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
MaxHeapify(a,1,i-1);
}
}
void MinHeapsort(int a[],int n)
{
int i,temp;
for(i=n;i>=2;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
MinHeapify(a,1,i-1);
}
}
void Build_MaxHeap(int a[],int n)
{
int i;
for(i=n/2;i>=1;i--)
MaxHeapify(a,i,n);
}
void Build_MinHeap(int a[],int n)
{
int i;
for(i=n/2;i>=1;i--)
MinHeapify(a,i,n);
}
int main()
{
int n,i;
printf("\n Enter the number of students: ");
scanf("%d",&n);
n++;
int arr[n];
for(i=1;i<n;i++)
{
printf("Enter the marks: %d ",i);
scanf("%d",&arr[i]);
}
Build_MaxHeap(arr,n-1);
MaxHeapsort(arr,n-1);
int max,min;
printf("\nSorted Data : ASCENDING:");
for(i=1;i<n;i++)
printf("%d->",arr[i]);
min=arr[1];
Build_MinHeap(arr,n-1);
MinHeapsort(arr,n-1);
printf("\nSorted Data : DESCENDING:");
max=arr[1];
for(i=1;i<n;i++)
printf("%d->",arr[i]);
printf("\n Maximum Marks : %d Minimum marks: %d",max,min);
}