Posts tagged ‘language’

February 1, 2011

Heap sorting.C++ Programming code


void Sift(int b[],int l,int r)
{
int i=l;
int x=b[i];
int j=2*i+1;
if((j<r)&&(b[j]<b[j+1])) j++;
while((j<=r)&&(x<b[j]))
{
b[i]=b[j];
i=j;
j=2*i+1;
if((j<r)&&(b[j]<b[j+1])) j++;
}
if(i!=l) b[i]=x;
}

void HeapSort(int m,int b[])
{
for(int i=m/2-1;i>=0;i–)
Sift(b,i,m-1);
for(i=m-1;i>0;)
{
int x=b[i];
b[i]=b[0];
b[0]=x;
Sift(b,0,–i);
}
}

Advertisements