Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Wednesday 8 May 2013

Heap Sort C Code And Algorithm

Heapsort is a comparison-based sorting algorithm to create a sorted array (or list), and is part of the Selection sort family. Although somewhat slower in practice on most machines than a well-implemented Quicksort, it has the advantage of a more favorable worst-case O(n log n) runtime. Heapsort is an in-place algorithm, but it is not a stable sort.


The heapsort algorithm can be divided into two parts.
In the first step, a heap is built out of the data.
In the second step, a sorted array is created by repeatedly removing the largest element from the heap, and inserting it into the array. The heap is reconstructed after each removal. Once all objects have been removed from the heap, we have a sorted array. The direction of the sorted elements can be varied by choosing a min-heap or max-heap in step one.
Heapsort can be performed in place. The array can be split into two parts, the sorted array and the heap.  The heap's invariant is preserved after each extraction, so the only cost is that of extraction.

Algorithm & Pseudo Code For Heap Sort:

/* Double-Click To Select Code */

function heapSort(a, count) is
     input:  an unordered array a of length count
     (first place a in max-heap order)
     heapify(a, count)
     end := count-1 
    /* In languages with zero-based arrays the children are 2*i+1 and 2*i+2 */
     while end > 0 do
         (swap the root(maximum value) of the heap with the last element of the heap)
         swap(a[end], a[0])
         (decrease the size of the heap by one so that the previous max value will
         stay in its proper placement) 
         end := end - 1
         (put the heap back in max-heap order)
         siftDown(a, 0, end)
 function heapify(a, count) is
     (start is assigned the index in a of the last parent node)
     start := (count - 1) / 2 
     while start ≥ 0 do
         (sift down the node at index start to the proper place such that all nodes
          below the start index are in heap order)
         siftDown(a, start, count-1)
         start := start - 1
     (after sifting down the root all nodes/elements are in heap order)
 function siftDown(a, start, end) is
     input:  end represents the limit of how far down the heap
                   to sift.
     root := start

     while root * 2 + 1 ≤ end do          (While the root has at least one child)
         child := root * 2 + 1        (root*2 + 1 points to the left child)
         swap := root        (keeps track of child to swap with)
         (check if root is smaller than left child)
         if a[swap] < a[child]
             swap := child
         (check if right child exists, and if it's bigger than what we're 
          currently swapping with)
         if child+1 ≤ end and a[swap] < a[child+1]
             swap := child + 1
         (check if we need to swap at all)
         if swap != root
             swap(a[root], a[swap])
             root := swap          (repeat to continue sifting down the child now)

C Program For Quick Sort:

/* Double-Click To Select Code */


void heapsort(int[], int);
void heapify(int[], int);
void adjust(int[], int);

int main()
 int array[50],n,i;

 printf("Enter the no. of elements to be sorted: ");

 printf("\nEnter the elements: \n");
 for(i=0 ; i<n ; i++)

 printf("\nBefore Heapsort:");  //Array Before Mergesort
 for(i = 0; i < n; i++)
  printf("%4d", array[i]);


 printf("\nAfter Heapsort:");  //Array After Mergesort
 for(i = 0; i < n; i++)
  printf("%4d", array[i]);
 return 0;

void heapsort(int array[], int n)
 int i,t;


 for(i=n-1 ; i>0 ; i--)
  t = array[0];
  array[0] = array[i];
  array[i] = t;

void heapify(int array[], int n)
 int item,i,j,k;

 for(k=1 ; k<n ; k++)
  item = array[k];
  i = k;
  j = (i-1)/2;

  while( (i>0) && (item>array[j]) )
   array[i] = array[j];
   i = j;
   j = (i-1)/2;
  array[i] = item;

void adjust(int array[], int n)
 int item,i,j;

 j = 0;
 item = array[j];
 i = 2*j+1;

  if(i+1 <= n-1)
   if(array[i] < array[i+1])
  if(item < array[i])
   array[j] = array[i];
   j = i;
   i = 2*j+1;
 array[j] = item;

Output Of Program:

Heap Sort C Code And Algorithm

Please Comment If You Liked This Post !! 

This article uses material from the Wikipedia article Heap_Sort which is released under the Creative Commons Attribution-Share-Alike License 3.0

Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: