void trickleDown(int index,int a[],int currentSize)
{
int largerChild;
int temp=a[index];
while(index<currentSize/2)
{
int leftChild=2*index+1;
int rightChild=leftChild+1;
if(rightChild<currentSize&&a[leftChild]<a[rightChild])
largerChild=rightChild;
else
largerChild=leftChild;
if(temp>=a[largerChild])
break;
a[index]=a[largerChild];
index=largerChild;
}
a[index]=temp;
}
void heapSort(int a[],int len)
{
int length=len;
for(int j=length/2-1;j>=0;j--)
trickleDown(j,a,length);
for(int j=length-1;j>=0;j--)
{
swap(a[0],a[j]);
trickleDown(0,a,j);
}
}
堆排序
最新推荐文章于 2025-03-24 22:59:14 发布