C program of Heap Sort Algorithm | C code champ

C program of Heap Sort Algorithm | C code champ


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.

  1. The first step is to build a heap out of the data.
  2. 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 :

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("----------------------Made by C code champ-----------------\n");
    printf("\t\tHEAP SORT ALGORITHM\n\n\n\n");
	printf("Enter the number of input : ");
	printf("\n\nEnter the elements to be sorted :\n");
	for ( i = 0; i < n; i++)
	printf("\n\nSorted List by Heap Sort :\n");
	for ( i = 0; i < n; i++)

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



  1. 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.

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

  3. 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.