C#堆排序完成辦法。本站提示廣大學習愛好者:(C#堆排序完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#堆排序完成辦法正文
本文實例講述了C#堆排序完成辦法。分享給年夜家供年夜家參考。詳細以下:
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); } }
願望本文所述對年夜家的C#法式設計有所贊助。