0% found this document useful (0 votes)
7 views4 pages

Practical 5

The document contains code for implementing heap sort in C. It includes functions for max heapifying a tree, building a max heap from an array, sorting the array using heap sort, and timing the sorting process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Practical 5

The document contains code for implementing heap sort in C. It includes functions for max heapifying a tree, building a max heap from an array, sorting the array using heap sort, and timing the sorting process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

5)

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
void maxheapify(int a[], int, int);
void maxheap(int a[],int beg,int end)
{
int i;
for(i=end/2;i>=beg;i--)
maxheapify(a,i,end);
}
void maxheapify(int a[],int f,int n)
{
int max,l,r,t;
max=f;
l=f*2;
r=(f*2)+1;
if(l<=n && a[l]>a[max])
{
max=l;
}
if(r<=n && a[r]>a[max])
{
max=r;
}
if(f!=max)
{
t=a[f];
a[f]=a[max];
a[max]=t;
maxheapify(a,max,n);
}
}
void heapsort(int a[],int n)
{
int i,t;
for(i=n;i>=2;i--)
{
t=a[1];
a[1]=a[i];
a[i]=t;
maxheapify(a,1,i-1);
}
}
void main()
{
int a[10],i,n;
struct time t1,t2;
clrscr();
printf("ET22BTIT084\n");
printf("Heap Sort!\n");
printf("Enter the number:");
scanf("%d",&n);
printf("Enter the element:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
gettime(&t1);
printf("\nStarting time:%d:%d:%d",t1.ti_hour,t1.ti_min,t1.ti_sec);
maxheap(a,1,n);
heapsort(a,n);
gettime(&t2);
printf("\nEnding time:%d:%d:%d",t2.ti_hour,t2.ti_min,t2.ti_sec);
printf("\nDifference:%d:%d:%d",t2.ti_hour-t1.ti_hour,t2.ti_min-t1.ti_min,t2.ti_sec-t1.ti_sec
);
printf("\nAfter sorting:");
for(i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
getch();
}
Output:

You might also like