//堆排序
#include
#include
using namespace std;
/*该函数完成对在数组R[low]到R[high]范围内,对在low上的结点进行调整(大堆)*/
void heapAdjust(int R[], int low, int high)
{
int i = low;
int j = i *2+1; //R[j]是R[i]的左孩子节点 (此时对应数组从0开始,若从1开始则 j=i*2)
int temp = R[i];
while (j < high)
{
if (j < high&&R[j] < R[j + 1])
++j; //若右孩子较大,则把 j 指向右孩子
if (temp < R[j])
{
R[i] = R[j]; //将 R[j] 调整到双亲节点的位置上
i = j; //修改 i 和 j 的值,以便继续向下调整
j = 2 * i+1;
}
else
break;
}
R[i] = temp;
}
堆排序问题代码
最新推荐文章于 2023-03-10 20:40:36 发布