软件编程
位置:首页>> 软件编程>> C#编程>> C#堆排序实现方法

C#堆排序实现方法

作者:令狐不聪  发布时间:2022-03-17 02:48:46 

标签: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#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com