void AdjustDown(int a[], int root, int size) //調整堆
{
int left = root * 2 + 1;
int right = left + 1;
int key = left;
while (left < size)
{
if (right<size&&a[left]>a[right])
{
key = right;
}
if (a[key] < a[root])
{
swap(a[root], a[key]);
root = key;
left = root * 2 + 1;
right = left + 1;
key = left;
}
else
{
break;
}
}
}
void HeapSort(int a[], int size)
{
int begin = size / 2 - 1;
for (; begin >= 0; begin--)
{
AdjustDown(a, begin, size);
}
int end = size - 1;
while (end)
{
swap(a[0], a[end]);
AdjustDown(a, 0, end);
end--;
}
}
堆排序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.