c# 排序之堆排序

闲聊 闲聊 1390 人阅读 | 0 人回复

<
代码:
  1. /// <summary>  
  2.         /// 堆排序办法。  
  3.         /// </summary>  
  4.         /// <param name="a">  
  5.         /// 待排序数组。  
  6.         /// </param>  
  7.         private void Heapsort(int[] a)
  8.         {
  9.             HeapSort_BuildMaxHeap(a); // 成立年夜根堆。  
  10.             Console.WriteLine("Build max heap:");
  11.             foreach (int i in a)
  12.             {
  13.                 Console.Write(i + " "); // 挨印年夜根堆。  
  14.             }
  15.             Console.WriteLine("\r\nMax heap in each iteration:");
  16.             for (int i = a.Length - 1; i > 0; i--)
  17.             {
  18.                 HeapSort_Swap(ref a[0], ref a[i]); // 将堆顶元素战无序区的最初一个元故旧换。  
  19.                 HeapSort_MaxHeaping(a, 0, i); // 将新的无序区调解为年夜根堆。  
  20.                 // 挨印每次堆排序迭代后的年夜根堆。  
  21.                 for (int j = 0; j < i; j++)
  22.                 {
  23.                     Console.Write(a[j] + " ");
  24.                 }
  25.                 Console.WriteLine(string.Empty);
  26.             }
  27.         }
  28.         /// <summary>  
  29.         /// 由底背上建堆。由完整两叉树的性子可知,叶子结面是从index=a.Length/2开端,以是从index=(a.Length/2)-1结面开端由底背长进止年夜根堆的调解。  
  30.         /// </summary>  
  31.         /// <param name="a">  
  32.         /// 待排序数组。  
  33.         /// </param>  
  34.         private static void HeapSort_BuildMaxHeap(int[] a)
  35.         {
  36.             for (int i = (a.Length / 2) - 1; i >= 0; i--)
  37.             {
  38.                 HeapSort_MaxHeaping(a, i, a.Length);
  39.             }
  40.         }
  41.         /// <summary>  
  42.         /// 将指定的结面调解为堆。  
  43.         /// </summary>  
  44.         /// <param name="a">  
  45.         /// 待排序数组。  
  46.         /// </param>  
  47.         /// <param name="i">  
  48.         /// 需求调解的结面。  
  49.         /// </param>  
  50.         /// <param name="heapSize">  
  51.         /// 堆的巨细,也指数组中无序区的少度。  
  52.         /// </param>  
  53.         private static void HeapSort_MaxHeaping(int[] a, int i, int heapSize)
  54.         {
  55.             int left = (2 * i) + 1; // 左子结面。  
  56.             int right = 2 * (i + 1); // 左子结面。  
  57.             int large = i; // 暂时变量,寄存年夜的结面值。  
  58.             // 比力左子结面。  
  59.             if (left < heapSize && a[left] > a[large])
  60.             {
  61.                 large = left;
  62.             }
  63.             // 比力左子结面。  
  64.             if (right < heapSize && a[right] > a[large])
  65.             {
  66.                 large = right;
  67.             }
  68.             // 若有子结面年夜于本身便交流,使年夜的元素上移;而且把该年夜的元素调解为堆以包管堆的性子。  
  69.             if (i != large)
  70.             {
  71.                 HeapSort_Swap(ref a[i], ref a[large]);
  72.                 HeapSort_MaxHeaping(a, large, heapSize);
  73.             }
  74.         }
  75.         /// <summary>  
  76.         /// 交流两个整数的值。  
  77.         /// </summary>  
  78.         /// <param name="a">整数a。</param>  
  79.         /// <param name="b">整数b。</param>  
  80.         private static void HeapSort_Swap(ref int a, ref int b)
  81.         {
  82.             int tmp = a;
  83.             a = b;
  84.             b = tmp;
  85.         }
复造代码
 

免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则