C# 堆排序示例代码
2018-07-20 来源:open-open
private static void Adjust (int[] list, int i, int m) { int Temp = list[i]; int j = i * 2 + 1; while (j <= m) { //more children if(j < m) if(list[j] < list[j + 1]) j = j + 1; //compare roots and the older children if(Temp < list[j]) { list[i] = list[j]; i = j; j = 2 * i + 1; } else { j = m + 1; } } list [i] = Temp; } public static void HeapSort (int[] list) { //build the initial heap for (int i = (list.Length - 1) / 2; i > = 0; i-) Adjust (list, i, list.Length - 1); //swap root node and the last heap node for (int i = list.Length - 1; i > = 1; i-) { int Temp = list [0]; list [0] = list [i]; list [i] = Temp; Adjust (list, 0, i - 1); } }
标签: swap
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐