1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Program.cs" company="Chimomo's Company">
  3. //
  4. // Respect the work.
  5. //
  6. // </copyright>
  7. // <summary>
  8. //
  9. // Heap sort.
  10. //
  11. // 堆排序是一种选择排序,时间复杂度为O(nlog<sub>2</sub>n)。
  12. // 堆排序的特点是:在排序过程中,将待排序数组看成是一棵全然二叉树的顺序存储结构,利用全然二叉树中父结点和子结点之间的内在关系,在当前无序区中选择keyword最大(或最小)的记录。

  13. //
  14. // 基本思想
  15. // 1.将待排序数组调整为一个大根堆。大根堆的堆顶元素就是这个堆中最大的元素。
  16. // 2.将大根堆的堆顶元素和无序区最后一个元素交换,并将无序区最后一个位置列入有序区,然后将新的无序区调整为大根堆。
  17. // 3.反复操作,直到无序区消失为止。
  18. // 初始时,整个数组为无序区。每一次交换,都是将大根堆的堆顶元素换入有序区,以保证有序区是有序的。

  19. //
  20. // </summary>
  21. // --------------------------------------------------------------------------------------------------------------------
  22. namespace CSharpLearning
  23. {
  24. using System;
  25. /// <summary>
  26. /// The program.
  27. /// </summary>
  28. public static class Program
  29. {
  30. /// <summary>
  31. /// 程序入口点。
  32. /// </summary>
  33. public static void Main()
  34. {
  35. int[] a = { 1, 14, 6, 2, 8, 66, 9, 3, 0, 10, 5, 34, 76, 809, 4, 7 };
  36. Console.WriteLine("Before Heap Sort...");
  37. foreach (int i in a)
  38. {
  39. Console.Write(i + " ");
  40. }
  41. Console.WriteLine("\r\n--------------------");
  42. Console.WriteLine("In Heap Sort...");
  43. HeapSort(a);
  44. Console.WriteLine("--------------------");
  45. Console.WriteLine("After Heap Sort...");
  46. foreach (int i in a)
  47. {
  48. Console.Write(i + " ");
  49. }
  50. Console.WriteLine(string.Empty);
  51. }
  52. /// <summary>
  53. /// 堆排序方法。
  54. /// </summary>
  55. /// <param name="a">
  56. /// 待排序数组。

  57. /// </param>
  58. private static void HeapSort(int[] a)
  59. {
  60. BuildMaxHeap(a); // 建立大根堆。
  61. Console.WriteLine("Build max heap:");
  62. foreach (int i in a)
  63. {
  64. Console.Write(i + " "); // 打印大根堆。
  65. }
  66. Console.WriteLine("\r\nMax heap in each iteration:");
  67. for (int i = a.Length - 1; i > 0; i--)
  68. {
  69. Swap(ref a[0], ref a[i]); // 将堆顶元素和无序区的最后一个元素交换。
  70. MaxHeaping(a, 0, i); // 将新的无序区调整为大根堆。
  71. // 打印每一次堆排序迭代后的大根堆。
  72. for (int j = 0; j < i; j++)
  73. {
  74. Console.Write(a[j] + " ");
  75. }
  76. Console.WriteLine(string.Empty);
  77. }
  78. }
  79. /// <summary>
  80. /// 由底向上建堆。由全然二叉树的性质可知,叶子结点是从index=a.Length/2開始。所以从index=(a.Length/2)-1结点開始由底向上进行大根堆的调整。
  81. /// </summary>
  82. /// <param name="a">
  83. /// 待排序数组。
  84. /// </param>
  85. private static void BuildMaxHeap(int[] a)
  86. {
  87. for (int i = (a.Length / 2) - 1; i >= 0; i--)
  88. {
  89. MaxHeaping(a, i, a.Length);
  90. }
  91. }
  92. /// <summary>
  93. /// 将指定的结点调整为堆。

  94. /// </summary>
  95. /// <param name="a">
  96. /// 待排序数组。

  97. /// </param>
  98. /// <param name="i">
  99. /// 须要调整的结点。

  100. /// </param>
  101. /// <param name="heapSize">
  102. /// 堆的大小,也指数组中无序区的长度。
  103. /// </param>
  104. private static void MaxHeaping(int[] a, int i, int heapSize)
  105. {
  106. int left = (2 * i) + 1; // 左子结点。

  107. int right = 2 * (i + 1); // 右子结点。
  108. int large = i; // 暂时变量,存放大的结点值。
  109. // 比較左子结点。
  110. if (left < heapSize && a[left] > a[large])
  111. {
  112. large = left;
  113. }
  114. // 比較右子结点。

  115. if (right < heapSize && a[right] > a[large])
  116. {
  117. large = right;
  118. }
  119. // 如有子结点大于自身就交换,使大的元素上移;而且把该大的元素调整为堆以保证堆的性质。

  120. if (i != large)
  121. {
  122. Swap(ref a[i], ref a[large]);
  123. MaxHeaping(a, large, heapSize);
  124. }
  125. }
  126. /// <summary>
  127. /// 交换两个整数的值。
  128. /// </summary>
  129. /// <param name="a">整数a。</param>
  130. /// <param name="b">整数b。

    </param>

  131. private static void Swap(ref int a, ref int b)
  132. {
  133. int tmp = a;
  134. a = b;
  135. b = tmp;
  136. }
  137. }
  138. }
  139. // Output:
  140. /*
  141. Before Heap Sort...
  142. 1 14 6 2 8 66 9 3 0 10 5 34 76 809 4 7
  143. --------------------
  144. In Heap Sort...
  145. Build max heap:
  146. 809 14 76 7 10 66 9 3 0 8 5 34 1 6 4 2
  147. Max heap in each iteration:
  148. 76 14 66 7 10 34 9 3 0 8 5 2 1 6 4
  149. 66 14 34 7 10 4 9 3 0 8 5 2 1 6
  150. 34 14 9 7 10 4 6 3 0 8 5 2 1
  151. 14 10 9 7 8 4 6 3 0 1 5 2
  152. 10 8 9 7 5 4 6 3 0 1 2
  153. 9 8 6 7 5 4 2 3 0 1
  154. 8 7 6 3 5 4 2 1 0
  155. 7 5 6 3 0 4 2 1
  156. 6 5 4 3 0 1 2
  157. 5 3 4 2 0 1
  158. 4 3 1 2 0
  159. 3 2 1 0
  160. 2 0 1
  161. 1 0
  162. 0
  163. --------------------
  164. After Heap Sort...
  165. 0 1 2 3 4 5 6 7 8 9 10 14 34 66 76 809
  166. */

算法 Heap sort的更多相关文章

  1. 【算法】堆排序(Heap Sort)(七)

    堆排序(Heap Sort) 堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法.堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父 ...

  2. 堆排序 Heap Sort

    堆排序虽然叫heap sort,但是和内存上的那个heap并没有实际关系.算法上,堆排序一般使用数组的形式来实现,即binary heap. 我们可以将堆排序所使用的堆int[] heap视为一个完全 ...

  3. Python入门篇-数据结构堆排序Heap Sort

    Python入门篇-数据结构堆排序Heap Sort 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.堆Heap 堆是一个完全二叉树 每个非叶子结点都要大于或者等于其左右孩子结点 ...

  4. Insert or Merge && Insertion or Heap Sort

    原题连接:https://pta.patest.cn/pta/test/1342/exam/4/question/27102 题目如下: According to Wikipedia: Inserti ...

  5. [Unity][Heap sort]用Unity动态演示堆排序的过程(How Heap Sort Works)

    [Unity][Heap sort]用Unity动态演示堆排序的过程 How Heap Sort Works 最近做了一个用Unity3D动态演示堆排序过程的程序. I've made this ap ...

  6. PTA Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. 09-排序3 Insertion or Heap Sort

    和前一题差不多,把归并排序换成了堆排序.要点还是每一次排序进行判断 开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误...一直在检查逻辑 建立最大堆 排 ...

  8. 1098. Insertion or Heap Sort (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)

    堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...

随机推荐

  1. Codeforces Beta Round #95 (Div. 2) C 组合数学

    C. The World is a Theatre time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. Java EE 学习:使用 idea2017 搭建 SSM 框架

    需要准备的环境: idea 2017.1 jdk1.8 Maven 3.3.9 请提前将idea与Maven.jdk配置好,本次项目用的都是比较新的 步骤: 一.首先使用idea新建一个Maven w ...

  3. 【转】手摸手,带你用vue撸后台 系列一

    前言 说好的教程终于来了,第一篇文章主要来说一说在开始写业务代码前的一些准备工作吧,但这里不会教你webpack的基础配置,热更新怎么做,webpack速度优化等等,有需求的请自行google. 目录 ...

  4. mysql source、mysqldump 导入导出数据(转)

    解决了mysql gbk编码的导入导出问题,感谢作者. 一.导入数据 1.确定 数据库默认编码,比如编码 为gbk,将读入途径编码同样设为gbk,命令为:           set names gb ...

  5. 【AtCoder Regular Contest 076 F】Exhausted (贪心)

    Description 机房里有M台电脑排成一排,第i台电脑的坐标是正整数i. 现在有N个OIer进入了机房,每个OIer需要一台电脑来学tui习ji,同时每个OIer对自己电脑所处的坐标范围有一个要 ...

  6. sourceInsight4 破解笔记(完美破解)【转】

    转自:http://www.cnblogs.com/Napoleon-Wang/p/6706773.html 软件下载地址:http://www.cr173.com/soft/421803.html ...

  7. 深入代码详谈irqbalance【转】

    转自:http://blog.csdn.net/whrszzc/article/details/50533866 版权声明:本文为博主原创文章,未经博主允许不得转载. 深入代码详谈irqbalance ...

  8. C#byte怎么转成图片

    这个其实很简单我给大家提供一个方法吧 /// <summary> /// 字节数组生成图片 /// </summary> /// <param name="By ...

  9. (入门SpringBoot)SpringBoot后台验证(八)

    后台验证的作用主要是防止postman...等等工具的恶意提交,前后台都判断数据,双保险. .可以在SpringBoot传参数 加上NotNull.... //分组Default,分组的好处就是可重复 ...

  10. Java Servlet Filter

    做web开发的人对于Filter应该不会陌生,一直在很简单的使用,但是一直没有系统的总结一下,随着年纪的慢慢长大,喜欢总结一些事情,下面说说我对Filter的理解,官方给出的Filter的定义是在请求 ...