堆排序(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#算法设计排序篇之09-基数排序(附带动画演示程序)

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

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

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

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

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

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

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

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

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

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

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

  8. C#算法设计排序篇之01-冒泡排序(附带动画演示程序)

    冒泡排序(Bubble Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/672 访问. 它重复地访问要排序的元 ...

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

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

随机推荐

  1. Serverless的概念&定义-无服务计算详解

    过去几年间,Serverless 发展迅猛,与其相伴的还有从小程序.移动端等到前后端一体化的演进与实践,也正因如此,从云计算到前端,众多开发者都极为关注 Serverless到底是什么? 在国内,Se ...

  2. Ethical Hacking - NETWORK PENETRATION TESTING(23)

    Detecting ARP Posionning Attacks ARP main security issues: 1. Each ARP requests/response is trusted. ...

  3. P1536 村村通(洛谷)并查集

    隔壁的dgdger带我看了看老师的LCA教程,我因为学习数学太累了(就是懒),去水了一下,感觉很简单的样子,于是我也来写(水)个博客吧. 题目描述 某市调查城镇交通状况,得到现有城镇道路统计表.表中列 ...

  4. SQL : 把特定的数据排前面 & 分别查询几组数据的最大值

    把特定的数据排前面 : 比如说,把没有审核身份证的人排最前面,然后再按userId正序排. select case when idcardverified = 1 then 0 else 1 end ...

  5. python Scrapy 从零开始学习笔记(一)

    在之前我做了一个系列的关于 python 爬虫的文章,传送门:https://www.cnblogs.com/weijiutao/p/10735455.html,并写了几个爬取相关网站并提取有效信息的 ...

  6. 互联网找的e是无理数的初等证明

    e的两种计算方式 \(e=lim_{n \to \infty}(1+\frac{1}{n})^n\) \(e=\sum_{n=0}^{+\infty}\frac{1}{n!}\) \(即,e=\fra ...

  7. CentOS7.3 ffmpeg安装

    ffmpeg安装笔记 ======================== 一.安装依赖 yum -y install yum-utils yum-config-manager --add-repo ht ...

  8. 理解k8s资源限制系列(二):cpu time

    本文介绍几种在K8S中限制资源使用的几种方法. 资源类型 在K8S中可以对两类资源进行限制:cpu和内存. CPU的单位有: 正实数,代表分配几颗CPU,可以是小数点,比如0.5代表0.5颗CPU,意 ...

  9. Java Servlet详解(体系结构+注解配置+生命周期)

    Java Servlet详解(注解配置+生命周期) 什么是Servlet : (Server applet)? 顾名思义:服务端的小程序 Servlet只是一个接口,定义了Java被浏览器访问到(To ...

  10. Ubuntu chmod 命令修改文件chmod读写权限

    Ubuntu chmod 命令可以用来修改文件或文件夹的读写权限 chmod 命令有两种使用方式 一. chmod [u/g/o/a] [+/-/=] [r/w/x] filename [ ]里都代表 ...