好久没写堆排序的代码了,现在熟悉一下:
void shitDowm(int *a ,int pos,int len)
{
if (a == NULL)
return ;
int temp ,child;
for(temp =a[pos];2*pos+1<len;pos = child)
{
child = 2*pos+1;
if(child <len-1 && a[child] <a[child+1])
child++;
if(temp <a[child])
a[pos] = a[child];
else
break;
}
a[pos] = temp;
}
void HeapSort(int *a ,int len)
{
if(a == NULL)
return ;
for(int i=len/2-1;i>=0;i--)
{
shitDowm(a,i,len);
}
for(int i=len-1;i>=0;i--)
{
swap(a[0],a[i]);
shitDowm(a,0,i);
}
}