我将堆大小设为 14,因为它是数组的初始大小,我正在做从算法 clrs 介绍开始的问题 6.2-1。我没有其他一些辅助功能,例如交换或“打印数组”
我对堆大小不是很清楚
void max_heapify(int arr[],int i){
int largest ;
int n = 14;
int left = 2*i;
int right = (2*i) + 1;
if (left <= n && arr[left] > arr[i])
{
largest = left;
}
else
{
largest = i;
}
if (right <= n && arr[right] > arr[i])
{
largest = right;
}
else
{
largest = i;
}
if(largest != i)
{
swap(arr[i], arr[largest]);
max_heapify(arr, largest);
}
}
int main()
{
int arr[] = { 27,17,3,16,13,10,1,5,7,12,4,8,9,0 };
max_heapify(arr, 3);
}