**C program of Heap Sort Algorithm** : **Heapsort** is a comparison based sorting algorithm to create a sorted array (or list), and is part of the selection sort family. Heap sort is actually a two step algorithm.

- The first step is to build a heap out of the data.
- The second step begins with removing the largest element from the heap. We insert the removed element into the sorted array. For the first element, this would be position zero of the array. Next we reconstruct the heap and remove the next largest item, and insert it into the array. After we have removed all the objects from the heap, we have a sorted array. We can vary the direction of the sorted elements by choosing a min-heap or max-heap in step one.

Let us see how to write a **C program of Heap Sort Algorithm**.

### C program of Heap Sort Algorithm :

#include<stdio.h> #include<conio.h> void siftDown(int numbers[], int root, int bottom) { int maxChild = root * 2 + 1; // Find the biggest child if(maxChild < bottom) { int otherChild = maxChild + 1; // Reversed for stability maxChild = (numbers[otherChild] > numbers[maxChild])?otherChild:maxChild; } else { // Don't overflow if(maxChild > bottom) return; } // If we have the correct ordering, we are done. if(numbers[root] >= numbers[maxChild]) return; // Swap int temp = numbers[root]; numbers[root] = numbers[maxChild]; numbers[maxChild] = temp; // Tail queue recursion. siftDown(numbers, maxChild, bottom); } void heapSort(int numbers[], int array_size) { int i, temp; for (i = (array_size / 2); i >= 0; i--) { siftDown(numbers, i, array_size - 1); } for (i = array_size-1; i >= 1; i--) { // Swap temp = numbers[0]; numbers[0] = numbers[i]; numbers[i] = temp; siftDown(numbers, 0, i-1); } } int main() { int n,i,k = 0, A[15]; printf("-----------------------------------------------------------\n"); printf("----------------------Made by C code champ-----------------\n"); printf("-----------------------------------------------------------\n\n"); printf("\t\tHEAP SORT ALGORITHM\n\n\n\n"); printf("Enter the number of input : "); scanf("%d",&n); printf("\n\nEnter the elements to be sorted :\n"); for ( i = 0; i < n; i++) { scanf("%d",&A[i]); } heapSort(A,n); printf("\n\nSorted List by Heap Sort :\n"); for ( i = 0; i < n; i++) { printf("%d\t",A[i]); } getch(); }

We hope that you all have enjoyed the **C program** for Heap Sort Algorithm. If you have any queries regarding above code, feel free to ask us in form of comments.

Definitely, what a splendid website and illuminating posts, I definitely will bookmark your blog.All the Best!

Hiya, I’m really glad I’ve found this info. Today bloggers publish only about gossips and net and this is really irritating. A good website with interesting content, that is what I need. Thanks for keeping this web site, I’ll be visiting it. Do you do newsletters? Can’t find it.

Hi, Neat post. There’s a problem with your site in web explorer, might test this? IE still is the marketplace chief and a big part of folks will leave out your great writing because of this problem.

I have been surfing online more than 3 hours today, yet I never found any interesting article like yours. Itâ€™s pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.

chanel coco http://chanelcoco.hpage.com/

I could relate entirely to what you were saying. You have to of been reading through my thoughts

Part of the equity release plan” can make an immediate and significant improvement” horsebox insurance to the quality of being fair and impartial.

Thus, before getting too far into this process, the options are discussed

with a financial expert or a lawyer who would be able to leave anything for your beneficiaries.

Tom Joule, who founded the company in 1989 selling socks, wellies and knitwear

at outdoor events, said the PCS.