堆排序(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. day5 python代码块,流程控制

    判断类型 # isinstance 用法一 isinstance(值,类型)--------------->bool isinstance(5,int)-----------------> ...

  2. ant-design-vue中实现modal模态框的复用(添加,编辑展示同一个模态框)

    用两个button(添加,编辑)按钮展示同一个模态框,并不是什么大问题,问题在于解决这两个模态框得有自己的确定和取消方法 父页面完全接管子页面(利于子页面复用) 父页面代码: <template ...

  3. “我放弃了年薪20万的offer…”

    最近身边朋友换工作.转型的越来越多.爬到一定高度,或者说到了一定年龄,每个选择都显得尤为重要.不仅因为高昂的机会成本,还有大家对后续规划的多重考虑.有一个说法你可能听过:混职场,要拥有不可替代的能力. ...

  4. RocketMQ在面试中那些常见问题及答案+汇总

    0.汇总 RocketMQ入门到入土(一)新手也能看懂的原理和实战! RocketMQ入门到入土(二)事务消息&顺序消息 从入门到入土(三)RocketMQ 怎么保证的消息不丢失? Rocke ...

  5. 怎么训练出一个NB的Prophet模型

    上篇<神器の争>主要是介绍Prophet的特点以及prophet入门的一些注意事项,但离真正的实际运用还有段距离.本篇主要讲解实际运用中Prophet调参的主要步骤以及一些本人实际经验. ...

  6. Unable to find a constructor that takes a String param or a valueOf() or fromString() method

    Unable to find a constructor that takes a String param or a valueOf() or fromString() method 最近在做服务的 ...

  7. Python网络数据采集PDF高清完整版免费下载|百度云盘

    百度云盘:Python网络数据采集PDF高清完整版免费下载 提取码:1vc5   内容简介 本书采用简洁强大的Python语言,介绍了网络数据采集,并为采集新式网络中的各种数据类型提供了全面的指导.第 ...

  8. spring notes

    ************************ nexus is a tool of warehouse managementfirst nexus search local warehous ,i ...

  9. 一个startforresult的例子

    https://blog.csdn.net/qq_32521313/article/details/52451364

  10. python获取主机IP,主机名

    获取主机内网,外网IP,主机名 代码如下: #!/usr/bin/env python #-*- coding:utf-8 -*- # author:Zeng Xianhe import socket ...