当前位置: 移动技术网 > IT编程>开发语言>c# > C#堆排序实现方法

C#堆排序实现方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网