Write a C program to implement Heap Sort.

Written by siddhartha. Posted in Sortings

Source Code

#include <stdio.h>
#include <malloc.h>

void main()
{
 int *x,i,n;
 int temp;
 void heap(int *,int);

 fflush(stdin);
 printf("Heap Sort");
 printf("Enter How many Numbers : ");
 scanf("%d",&n);
 x = (int *)malloc(n * sizeof(int));
 printf("Enter the elements into the array :");
 for(i=0;i=1;i--)
 {
  temp =  x[i];
  x[i] = x[0];
  x[0] = temp;
  heap(x,i-1);
 }

 printf("The array after sorting is \n");
 for(i=0;i=0;i--)
 {
  if(a[(2*i)+1] < a[(2*i)+2] && (2*i+1)<=n && (2*i+2)<=n)
   {
    temp = a[(2*i)+1];
    a[(2*i)+1] = a[(2*i)+2];
    a[(2*i)+2] = temp;
   }
  if(a[(2*i)+1] > a[i] && (2*i+1)<=n && i<=n)
  {
   temp = a[(2*i)+1];
   a[(2*i)+1] = a[i];
   a[i] = temp;
  }
 }
}

Out Put

BLOG COMMENTS POWERED BY DISQUS