0% found this document useful (0 votes)
30 views

Heap Sort Algorithm

The document describes a C program that sorts an array using the heap sort algorithm. It includes the full source code of the program. The program takes user input for the number of elements and values to sort. It then performs heap sort by building a max heap from the array and repeatedly swapping the first element with the last unsorted element while max-heapifying the reduced subtree.

Uploaded by

Whow1985
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)
30 views

Heap Sort Algorithm

The document describes a C program that sorts an array using the heap sort algorithm. It includes the full source code of the program. The program takes user input for the number of elements and values to sort. It then performs heap sort by building a max heap from the array and repeatedly swapping the first element with the last unsorted element while max-heapifying the reduced subtree.

Uploaded by

Whow1985
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/ 2

C Program sorts an array based on heap sort algorithm.

source code of the C Program to sort an array based on heap sort algorithm. The C program is
successfully compiled and run on a Linux system. The program output is also shown below
1. #include <stdio.h>
2. void main()
3. {
4.
int heap[10], no, i, j, c, root, temp;
5.
printf("\n Enter no of elements :");
scanf("%d", &no);
6.
printf("\n Enter the nos : ");
7.
for (i = 0; i < no; i++)
scanf("%d", &heap[i]);
8.
for (i = 1; i < no; i++)
9.
{
10.
c = i;
11.
do
12.
{
13.
root = (c - 1) / 2;
14.
if (heap[root] < heap[c])
/* to create MAX heap array */
15.
{
16.
temp = heap[root]; heap[root] = heap[c]; heap[c] = temp;
17.
}
18.
c = root;
19.
} while (c != 0);
20.
}
21.
22.
printf("Heap array : ");
23.
for (i = 0; i < no; i++)
printf("%d\t ", heap[i]);
24.
for (j = no - 1; j >= 0; j--)
25.
{
26.
temp = heap[0];
27.
heap[0] = heap[j
/* swap max element with rightmost leaf
element */
28.
heap[j] = temp;
29.
root = 0;
30.
do
31.
{
32.
c = 2 * root + 1;
/* left node of root element */
33.
if ((heap[c] < heap[c + 1]) && c < j-1)
34.
c++;
35.
if (heap[root]<heap[c] && c<j)/* again rearrange to max heap array */
36.
{
37.
temp = heap[root];
38.
heap[root] = heap[c];
39.
heap[c] = temp;
40.
}
41.
root = c;
42.
} while (c < j);
43.
}
44.
printf("\n The sorted array is : ");
45.
for (i = 0; i < no; i++)
printf("\t %d", heap[i]);
46. }
$ cc heap.c $ a.out Enter no of elements :7

Program 2
#include<stdio.h>
void heapsort(int[],int);void heapify(int[],int);void adjust(int[],int);
main()
{
int n,i,a[50];
system("clear");
printf("\nEnter the limit:"); scanf("%d",&n);
printf("\nEnter the elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]);
heapsort(a,n);
printf("\nThe Sorted Elements Are:\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");
}
void heapsort(int a[],int n)
{
int i,t;
heapify(a,n);
for(i=n-1;i>0;i--)
{
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a,i);
}
}
void heapify(int a[],int n)
{
int k,i,j,item;
for(k=1;k<n;k++)
{
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j]))
{
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}}
void adjust(int a[],int n)
{
int i,j,item; j = 0; item = a[j]; i = 2*j+1;
while(i<=n-1)
{
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i])
{
a[j] = a[i];
j = i;
i = 2*j+1;
}
else
break;
}
a[j] = item;
}

You might also like