void MaxHeapify(vector<int> &Heap,int i)
{
int left=i;
int right=i;
int temp;
int largest=i;
while(i<Heap.size())
{
left=i<<1;
right=(i<<1)+1;
largest=i;
if(left<=Heap.size() && Heap[left]>Heap[i])
largest=left;
if(right<=Heap.size() && Heap[right]>Heap[i])
largest=right;
if(i!=largest)
{
temp=Heap[i];
Heap[i]=Heap[largest];
Heap[largest]=temp;
i=largest;
}
else
break;
}
}
习题 6.2-5 保持堆的性质 (迭代)
最新推荐文章于 2023-06-10 15:35:49 发布