# Heap Sort Algorithm

Heap sort, Heap sort algorithm, implementing Heap sort
Write a C program for implementing Heap sort algorithm for sorting a given list of integers in ascending order.

Globally declaring int a[10], n, i, j, c, root, temp;

1. Algorithm:
void maxheap()
Step1: Start
Step2: repeat from step 2 to step 11
for (i = 1; i < n; i++)
Step3: c = i
Step4: repeat from step 5 to step to step 11
do
Step5: root = (c - 1) / 2;
Step6: check root less than c upto step 9
if (a[root] < a[c]) /* to create MAX heap array */
Step7: temp = a[root]
Step8: a[root] = a[c]
Step9: a[c] = temp;
Step10: c = root;
Step11: while (c != 0);
Step12 print Max Heap array a[i]
Step13: stop

2. Algorithm:
main()
Step1: start
Step2: Read n no of elements
Step3: Read the array of elements a[i]
Step4: Call sub program maxheap()
Step5: Repeat from step 6 to step 19
for (j = n - 1; j >= 0; j--)
Step6: temp = a[0];

Step7: a[0] = a[j]; /* swap max element with rightmost leaf element*/
Step8: a[j] = temp;
Step9: root = 0;
Step10: Repeat from step 11 to step 19 do
Step11: c = 2 * root + 1; /* left node of root element */
Step12: Check if ((a[c] < a[c + 1]) && c < j-1)
Step13: c++;
Step14: Check if (a[root]<a[c] && c<j) upto Step 17
Step15: temp = a[root]
Step16: a[root] = a[c]
Step17: a[c] = temp
Step18: root = c
Step19: while (c < j);
Step20: print The sorted array is :a[i]
Step21: Stop

Thanks
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..