News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Heap sort

Started by aruljothi, Mar 31, 2009, 10:53 AM

Previous topic - Next topic

aruljothi

/*      Heap Sort   */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

void main()
{
int *x,i,n;
int temp;
void heap(int *,int);
clrscr();
fflush(stdin);
printf("
      Heap Sort
");
printf("
Enter How many Numbers : ");
scanf("%d",&n);
x = (int *)malloc(n * sizeof(int));
for(i=0;i<n;i++)
{fflush(stdin);
  scanf("%d",&x);
}
heap(x,n);

for(i=n-1;i>=1;i--)
{
  temp =  x;
  x = x[0];
  x[0] = temp;
  heap(x,i-1);
}

printf("
Resultant Array
");
for(i=0;i<n;i++)
  printf("%d   ",x);
free(x);
getch();
}


void heap(int *a,int n)
{
int i,temp;
for(i=n/2;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 && (2*i+1)<=n && i<=n)
  {
   temp = a[(2*i)+1];
   a[(2*i)+1] = a;
   a = temp;
  }
}
}