Percolate-down/Shift-down操作とHeapify操作の違いは何ですか?Heapsort - Percolate-down/Shift-down操作とHeapify操作の違い/関係は何ですか?
これはCのShift-down関数です。このコードを使用してHeapsortを実装しました。
void IterativeShiftDown(int i)
{
int leftOrRightChildIndex = EMPTY;
int currentIndex = i;
int currentElement = EMPTY;
int childElement = EMPTY;
while(HasAnyChild(currentIndex))
{
if(HasBothChild(currentIndex))
{
if(GetLeftChildElement(currentIndex) > GetRightChildElement(currentIndex))
{
leftOrRightChildIndex = GetLeftChildIndex(currentIndex);
}
else
{
leftOrRightChildIndex = GetRightChildIndex(currentIndex);
}
}
else if(HasLeftChild(currentIndex))
{
leftOrRightChildIndex = GetLeftChildIndex(currentIndex);
}
currentElement = GetElement(currentIndex);
childElement = GetElement(leftOrRightChildIndex);
if(currentElement < childElement)
{
SwapElements(currentIndex, leftOrRightChildIndex);
}
currentIndex = leftOrRightChildIndex;
}
}
このコードを使用してHeapify()関数を作成できますか?
はいの場合はどうですか?
また、Heapifyを使用してHeaportを実装するアルゴリズムとは何ですか?