C#堆排序实现方法
更新时间:2015年04月02日 10:38:18 作者:令狐不聪
这篇文章主要介绍了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#程序设计有所帮助。
您可能感兴趣的文章:
相关文章
c#高效的线程安全队列ConcurrentQueue<T>的实现
这篇文章主要介绍了c#高效的线程安全队列ConcurrentQueue<T>的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-11-11
C#在WinForm中使用WebKit传递js对象实现与网页交互的方法
这篇文章主要介绍了C#在WinForm中使用WebKit传递js对象实现与网页交互的方法,涉及针对WebBroswer控件及WebKit控件的相关使用技巧,需要的朋友可以参考下2016-03-03


最新评论