算法 Heap sort
- // --------------------------------------------------------------------------------------------------------------------
- // <copyright file="Program.cs" company="Chimomo's Company">
- //
- // Respect the work.
- //
- // </copyright>
- // <summary>
- //
- // Heap sort.
- //
- // 堆排序是一种选择排序,时间复杂度为O(nlog<sub>2</sub>n)。
- // 堆排序的特点是:在排序过程中,将待排序数组看成是一棵全然二叉树的顺序存储结构,利用全然二叉树中父结点和子结点之间的内在关系,在当前无序区中选择keyword最大(或最小)的记录。
- //
- // 基本思想
- // 1.将待排序数组调整为一个大根堆。大根堆的堆顶元素就是这个堆中最大的元素。
- // 2.将大根堆的堆顶元素和无序区最后一个元素交换,并将无序区最后一个位置列入有序区,然后将新的无序区调整为大根堆。
- // 3.反复操作,直到无序区消失为止。
- // 初始时,整个数组为无序区。每一次交换,都是将大根堆的堆顶元素换入有序区,以保证有序区是有序的。
- //
- // </summary>
- // --------------------------------------------------------------------------------------------------------------------
- namespace CSharpLearning
- {
- using System;
- /// <summary>
- /// The program.
- /// </summary>
- public static class Program
- {
- /// <summary>
- /// 程序入口点。
- /// </summary>
- public static void Main()
- {
- int[] a = { 1, 14, 6, 2, 8, 66, 9, 3, 0, 10, 5, 34, 76, 809, 4, 7 };
- Console.WriteLine("Before Heap Sort...");
- foreach (int i in a)
- {
- Console.Write(i + " ");
- }
- Console.WriteLine("\r\n--------------------");
- Console.WriteLine("In Heap Sort...");
- HeapSort(a);
- Console.WriteLine("--------------------");
- Console.WriteLine("After Heap Sort...");
- foreach (int i in a)
- {
- Console.Write(i + " ");
- }
- Console.WriteLine(string.Empty);
- }
- /// <summary>
- /// 堆排序方法。
- /// </summary>
- /// <param name="a">
- /// 待排序数组。
- /// </param>
- private static void HeapSort(int[] a)
- {
- BuildMaxHeap(a); // 建立大根堆。
- Console.WriteLine("Build max heap:");
- foreach (int i in a)
- {
- Console.Write(i + " "); // 打印大根堆。
- }
- Console.WriteLine("\r\nMax heap in each iteration:");
- for (int i = a.Length - 1; i > 0; i--)
- {
- Swap(ref a[0], ref a[i]); // 将堆顶元素和无序区的最后一个元素交换。
- MaxHeaping(a, 0, i); // 将新的无序区调整为大根堆。
- // 打印每一次堆排序迭代后的大根堆。
- for (int j = 0; j < i; j++)
- {
- Console.Write(a[j] + " ");
- }
- Console.WriteLine(string.Empty);
- }
- }
- /// <summary>
- /// 由底向上建堆。由全然二叉树的性质可知,叶子结点是从index=a.Length/2開始。所以从index=(a.Length/2)-1结点開始由底向上进行大根堆的调整。
- /// </summary>
- /// <param name="a">
- /// 待排序数组。
- /// </param>
- private static void BuildMaxHeap(int[] a)
- {
- for (int i = (a.Length / 2) - 1; i >= 0; i--)
- {
- MaxHeaping(a, i, a.Length);
- }
- }
- /// <summary>
- /// 将指定的结点调整为堆。
- /// </summary>
- /// <param name="a">
- /// 待排序数组。
- /// </param>
- /// <param name="i">
- /// 须要调整的结点。
- /// </param>
- /// <param name="heapSize">
- /// 堆的大小,也指数组中无序区的长度。
- /// </param>
- private static void MaxHeaping(int[] a, int i, int heapSize)
- {
- int left = (2 * i) + 1; // 左子结点。
- int right = 2 * (i + 1); // 右子结点。
- int large = i; // 暂时变量,存放大的结点值。
- // 比較左子结点。
- if (left < heapSize && a[left] > a[large])
- {
- large = left;
- }
- // 比較右子结点。
- if (right < heapSize && a[right] > a[large])
- {
- large = right;
- }
- // 如有子结点大于自身就交换,使大的元素上移;而且把该大的元素调整为堆以保证堆的性质。
- if (i != large)
- {
- Swap(ref a[i], ref a[large]);
- MaxHeaping(a, large, heapSize);
- }
- }
- /// <summary>
- /// 交换两个整数的值。
- /// </summary>
- /// <param name="a">整数a。</param>
- /// <param name="b">整数b。
</param>
- private static void Swap(ref int a, ref int b)
- {
- int tmp = a;
- a = b;
- b = tmp;
- }
- }
- }
- // Output:
- /*
- Before Heap Sort...
- 1 14 6 2 8 66 9 3 0 10 5 34 76 809 4 7
- --------------------
- In Heap Sort...
- Build max heap:
- 809 14 76 7 10 66 9 3 0 8 5 34 1 6 4 2
- Max heap in each iteration:
- 76 14 66 7 10 34 9 3 0 8 5 2 1 6 4
- 66 14 34 7 10 4 9 3 0 8 5 2 1 6
- 34 14 9 7 10 4 6 3 0 8 5 2 1
- 14 10 9 7 8 4 6 3 0 1 5 2
- 10 8 9 7 5 4 6 3 0 1 2
- 9 8 6 7 5 4 2 3 0 1
- 8 7 6 3 5 4 2 1 0
- 7 5 6 3 0 4 2 1
- 6 5 4 3 0 1 2
- 5 3 4 2 0 1
- 4 3 1 2 0
- 3 2 1 0
- 2 0 1
- 1 0
- 0
- --------------------
- After Heap Sort...
- 0 1 2 3 4 5 6 7 8 9 10 14 34 66 76 809
- */
算法 Heap sort的更多相关文章
- 【算法】堆排序(Heap Sort)(七)
堆排序(Heap Sort) 堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法.堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父 ...
- 堆排序 Heap Sort
堆排序虽然叫heap sort,但是和内存上的那个heap并没有实际关系.算法上,堆排序一般使用数组的形式来实现,即binary heap. 我们可以将堆排序所使用的堆int[] heap视为一个完全 ...
- Python入门篇-数据结构堆排序Heap Sort
Python入门篇-数据结构堆排序Heap Sort 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.堆Heap 堆是一个完全二叉树 每个非叶子结点都要大于或者等于其左右孩子结点 ...
- Insert or Merge && Insertion or Heap Sort
原题连接:https://pta.patest.cn/pta/test/1342/exam/4/question/27102 题目如下: According to Wikipedia: Inserti ...
- [Unity][Heap sort]用Unity动态演示堆排序的过程(How Heap Sort Works)
[Unity][Heap sort]用Unity动态演示堆排序的过程 How Heap Sort Works 最近做了一个用Unity3D动态演示堆排序过程的程序. I've made this ap ...
- PTA Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 09-排序3 Insertion or Heap Sort
和前一题差不多,把归并排序换成了堆排序.要点还是每一次排序进行判断 开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误...一直在检查逻辑 建立最大堆 排 ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)
堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...
随机推荐
- 【VBA】全局数组定义
[说明] 全局数组定义(写在Module的最上面) 'Array Public Arr_approver Public Arr_delegator Public Arr_Role
- 获取浏览器的homepage
主要知识点:跨进程访问数据 首先修改浏览器源码:BrowserSettings.java private static String getSDMCDefaultSharedPreferencesNa ...
- java运行时间计算
long startTime = System.currentTimeMillis(); //获取开始时间 doSomething(); //测试的代码段 long endTime = System. ...
- Origin 2018 的坐标轴中文标签发生倒立/翻转
问题: 在使用 origin 2018 作图时,坐标轴或者是标签中输入中文后,将其更换中文字体(英文字体没有该问题)后发生倒立的情况 解决: 后来发现 Origin 2018 中存在两种中文字体,其中 ...
- Eclipse与MyEclipse增强代码提示
Eclipse默认是敲入"."之后有代码提示,但假如我们想在敲入类似sys有System的提示,则需要修改一下Eclispe的配置来达到这个效果. 具体配置方法如下: 点Windo ...
- js scrollTop, 滚动条操作
设置页面加载时滚动条自动滚到底的方法: jQuery: 复制代码 代码如下: $(function(){ var h = $(document).height()-$(window).height() ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- LeetCode OJ--Palindrome Partitioning **
https://oj.leetcode.com/problems/palindrome-partitioning/ 给定一个字符串 s,求所有的子串组合,每个子串都是回文的. 比如,aba: {a,b ...
- Codeforces Gym100735 G.LCS Revised (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
G.LCS Revised The longest common subsequence is a well known DP problem: given two strings A and B ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...