堆排序(Heap Sort)

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/685 访问。

堆排序是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。可以利用数组的特点快速定位指定索引的元素。堆分为大根堆和小根堆,是完全二叉树。大根堆的要求是每个节点的值都不大于其父节点的值,即A[PARENT[i]] >= A[i]。在数组的升序排序中,需要使用的就是大根堆,因为根据大根堆的要求可知,最大的值一定在堆顶。


示例: 

public class Program {public static void Main(string[] args) {int[] array = { 43, 69, 11, 72, 28, 21, 56, 80, 48, 94, 32, 8 };HeapSort(array);ShowSord(array);Console.ReadKey();}private static void ShowSord(int[] array) {foreach (var num in array) {Console.Write($"{num} ");}Console.WriteLine();}private static void HeapSort(int[] array) {MaxHeap(array);for (int i = array.Length - 1; i > 0; i--) {Swap(ref array[0], ref array[i]);Heapify(array, 0, i);}}private static void MaxHeap(int[] array) {for (int i = array.Length / 2 - 1; i >= 0; i--) {Heapify(array, i, array.Length);}}private static void Heapify(int[] array, int index, int size) {int left = 2 * index + 1;int right = 2 * index + 2;int large = index;if (left < size && array[left] > array[large]) {large = left;}if (right < size && array[right] > array[large]) {large = right;}if (index != large) {Swap(ref array[index], ref array[large]);Heapify(array, large, size);}}private static void Swap(ref int first, ref int second) {int t = first;first = second;second = t;}}

以上是堆排序算法的一种实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/685 访问。

8 11 21 28 32 43 48 56 69 72 80 94

分析:

堆排序算法的时间复杂度不难证明为:  。


AlgorithmMan:

AlgorithmMan by Iori,AlgorithmMan是使用C#开发的一套用于算法演示的工具。

下载链接:AlgorithmMan-HeapSort

C#算法设计排序篇之06-堆排序(附带动画演示程序)相关推荐

  1. C#算法设计排序篇之04-选择排序(附带动画演示程序)

    选择排序(Selection Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/681 访问. 选择排序是一种简 ...

  2. C#算法设计排序篇之10-桶排序(附带动画演示程序)

    桶排序(Bucket Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/693 访问. 桶排序的工作原理是将数组 ...

  3. C#算法设计排序篇之11-二叉树排序(附带动画演示程序)

    二叉树排序(Binary Tree Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/695 访问. 二叉树排序 ...

  4. C#算法设计排序篇之09-基数排序(附带动画演示程序)

    基数排序(Radix Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/691 访问. 基数排序属于" ...

  5. C#算法设计排序篇之08-计数排序(附带动画演示程序)

    计数排序(Counting Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/689 访问. 计数排序是一个非基 ...

  6. C#算法设计排序篇之07-希尔排序(附带动画演示程序)

    希尔排序(Shell's Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/687 访问. 希尔排序是插入排序的 ...

  7. C#算法设计排序篇之05-归并排序(附带动画演示程序)

    归并排序(Merge Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/683 访问. 归并排序是建立在归并操作 ...

  8. C#算法设计排序篇之03-直接插入排序(附带动画演示程序)

    直接插入排序(Straight Insertion Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/679 访 ...

  9. C#算法设计排序篇之02-快速排序(附带动画演示程序)

    快速排序(Quick Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/677 访问. 快速排序由C. A. R ...

最新文章

  1. Level3公司在哥伦比亚开通运营第三个数据中心
  2. MySQL 5.1.40新特性及下载
  3. springboot ftp 笔记
  4. linux du命令使用:目录大小排序
  5. SAP 3D visual enterprise viewer 9.0 的下载方式
  6. SAP IBASE logic of pt_equiindx
  7. EF Core 3 的 40 个中断性变更
  8. 13 登陆_《星球大战:弹珠台》中文版即将登陆Switch 12月13日正式发售
  9. linux 设备模型详解,Linux2.6 设备模型之input子系统详解
  10. linux nginx rtmp 直播,linux下利用nginx搭建rtmp直播服务
  11. 紫光华宇拼音输入法 6.x bug集
  12. 修改hosts文件在本地调试微信JS-SDK
  13. 小程序中纯CSS实现仿京东小优惠券图标
  14. 扫二维码 下载app
  15. idea修改代码提示的快捷键
  16. 不管过去如何,未来我们都要奋力前行!
  17. 十一、opencv-python图像处理顶级操作(3)——SIFT/SURF算法
  18. VirtualLab基础实验教程-3.迈克尔逊干涉仪
  19. 【生产调度优化】使用simulink实现生产调度优化
  20. 音视频开发录制功能 3GP格式的开发

热门文章

  1. Pycharm设置utf-8自动显示
  2. C++—— cin输入流详解
  3. HDU3788 ZOJ问题
  4. css3的新增选择器 200303
  5. 办公自动化-python编写ppt-创建第一页-主标题与内容的编写
  6. linux-查看文件类型-看本质-file
  7. 关于我对于写博客写文章的理解
  8. 如何使用Docker安装Redis\Zookeeper\Mysql
  9. android模拟按键问题总结[使用IWindowManager.injectKeyEvent方法](转)
  10. WinJS实用开发技巧(5):学习资料推荐