0% found this document useful (0 votes)
7 views1 page

Heap Sort

The document contains a C program that implements the heapsort algorithm. It includes functions for adjusting the heap, heapifying an array, and sorting the array using heapsort. The main function takes user input for the number of elements and the elements themselves, then sorts and prints them.

Uploaded by

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

Heap Sort

The document contains a C program that implements the heapsort algorithm. It includes functions for adjusting the heap, heapifying an array, and sorting the array using heapsort. The main function takes user input for the number of elements and the elements themselves, then sorts and prints them.

Uploaded by

colab178178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include<stdio.

h>
void adjust(int i,int n,int a[])
{
int j,item;
item=a[i];
j=i*2;
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
j++;
if(item>a[j])
break;
else
{
a[j/2]=a[j];
j=j*2;
}
}
a[j/2]=item;
}
void heapify(int a[],int n)
{
for(int i=n/2;i>0;i--)
adjust(i,n,a);
}

void heapsort(int a[],int n)


{
int temp,i;
heapify(a,n);
for(i=n;i>0;i--)
{
temp=a[1];
a[1]=a[i];
a[i]=temp;
adjust(1,i-1,a);
}
}
int main()
{
int n,i,a[20];
printf("enter no of elements");
scanf("%d",&n);
printf("enter elmemnts");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
heapsort(a,n);
for(i=1;i<=n;i++)
{
printf("%d\t",a[i]);
}
}

You might also like