依次向前, vCount);}// 不断输出堆顶元素、重构堆。
形成整个二叉堆)for (int i = vCount / 2; i = 1; i--){Restore(tempKey, i,。
1。
int nodeCount){while (rootNode = nodeCount / 2) // 保证根结点有子树{//找出左右儿子的最大值int m = (2 * rootNode + 1 = nodeCount arr[2 * rootNode + 1] arr[2 * rootNode]) ? 2 * rootNode + 1 : 2 * rootNode;if (arr[m] arr[rootNode]){int temp = arr[m];arr[m] = arr[rootNode];arr[rootNode] = temp;rootNode = m;}else{break;}}} 艾孜尔江 bju***ft@sina.com 6年前 (2020-10-08) , int rootNode, i - 1);}//排序结果for (int i = 0; i vCount; i++){arr[i] = tempKey[i + 1];}}/// summary/// 二叉堆的重构(针对于已构建好的二叉堆首尾互换之后的重构)/// /summary/// param/param/// param根结点j/param/// param结点数/paramstatic void Restore(int[] arr,进行排序for (int i = vCount; i 1; i--){int temp = tempKey[i];tempKey[i] = tempKey[1];tempKey[1] = temp;Restore(tempKey, 上方又没些 C# 的堆排序。
艾孜尔江补充如下: /// summary/// 堆排序/// /summary/// param待排序数组/paramstatic void HeapSort(int[] arr){int vCount = arr.Length;int[] tempKey = new int[vCount + 1];// 元素索引从1开始for (int i = 0; i vCount; i++){tempKey[i + 1] = arr[i];}// 初始数据建堆(从含最后一个结点的子树开始构建。
