0% found this document useful (0 votes)
24 views3 pages

Experiment No - Write A Program To Implement Heap Sort

The document describes implementing heap sort by: 1) Defining functions to swap elements, create a max heap by recursively placing elements in the correct position, and delete the max element from the heap by swapping with the last element and adjusting positions. 2) Calling the create heap function in a loop to build the max heap from an input array, then calling delete heap in a loop to extract elements in sorted order. 3) Printing the sorted array.

Uploaded by

shubham hira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Experiment No - Write A Program To Implement Heap Sort

The document describes implementing heap sort by: 1) Defining functions to swap elements, create a max heap by recursively placing elements in the correct position, and delete the max element from the heap by swapping with the last element and adjusting positions. 2) Calling the create heap function in a loop to build the max heap from an input array, then calling delete heap in a loop to extract elements in sorted order. 3) Printing the sorted array.

Uploaded by

shubham hira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment no –

Write a program to implement heap sort.


#include<stdio.h>
void swap(int *t,int *p)
{
int loc;
loc=*t;
*t=*p;
*p=temp;
}
void make_heap(int a[],int j)
{
int son=j;
int father=(s-1)/2;
while(father>=0)
{
if(a[son]>a[father])
{
swap(&a[son],&a[father]);
}
else
{
break;
}
son=father;
father=(son-1)/2;
}
}
void delete_heap(int a[],int j)
{swap(&a[j],&a[0]);
int father=0;
int son;
int rs=2*f+1,ls=2*f+2;
if(a[ls]>a[rs])
{
son=ls;
}
else
{
son=rs;
}
while(son<j)
{
if(a[son]>a[father])
{
swap(&a[son],&a[father]);
}
else
{
father=son;
int rs=2*father+1;
int ls=2*father+2;
if(a[ls]>a[rs])
{
son=ls;
}
else
{
s=rs;
}
}
}
}
void main()
{
int a[5] = {12, 11,14,9,8};
int j;
for(j=0;j<=4;j++)
{
create_heap(a,j);
}
for(j=4;j>0;j--)
{
delete_heap(a,j);
}
printf("Sorted elements\n");
for(j=0;j<=4;j++)
{
printf("%d\t",a[j]);
}}

You might also like