HeapSort 堆排序 基于伪代码实现
此文原创, http://www.cnblogs.com/baokang/p/4735431.html ,禁止转载
GIF 动态图

伪代码
/* From Wikipedia, the free encyclopedia */
1.父子节点特征
iParent = floor((i-1) / 2);
iLeftChild = 2*i + 1;
iRightChild = 2*i + 2;
2.算法伪代码
/* 保持原汁原味就不翻译了 =。= */
procedure heapsort(a, count) is
input: an unordered array a of length count (Build the heap in array a so that largest value is at the root)
heapify(a, count) (The following loop maintains the invariants that a[0:end] is a heap and every element
beyond end is greater than everything before it (so a[end:count] is in sorted order))
end ← count - 1
while end > 0 do
(a[0] is the root and largest value. The swap moves it in front of the sorted elements.)
swap(a[end], a[0])
(the heap size is reduced by one)
end ← end - 1
(the swap ruined the heap property, so restore it)
siftDown(a, 0, end)
(Put elements of 'a' in heap order, in-place)
procedure heapify(a, count) is
(start is assigned the index in 'a' of the last parent node)
(the last element in a 0-based array is at index count-1; find the parent of that element)
start ← floor ((count - 2) / 2) while start ≥ 0 do
(sift down the node at index 'start' to the proper place such that all nodes below
the start index are in heap order)
siftDown(a, start, count - 1)
(go to the next parent node)
start ← start - 1
(after sifting down the root all nodes/elements are in heap order) (Repair the heap whose root element is at index 'start', assuming the heaps rooted at its children are valid)
procedure siftDown(a, start, end) is
root ← start while root * 2 + 1 ≤ end do (While the root has at least one child)
child ← root * 2 + 1 (Left child)
swap ← root (Keeps track of child to swap with) if a[swap] < a[child]
swap ← child
(If there is a right child and that child is greater)
if child+1 ≤ end and a[swap] < a[child+1]
swap ← child + 1
if swap = root
(The root holds the largest element. Since we assume the heaps rooted at the
children are valid, this means that we are done.)
return
else
swap(a[root], a[swap])
root ← swap (repeat to continue sifting down the child now)
Java实现
public void heapsort(int[] a, int count) {
if (count < 2)
return;
heapify(a, count);
int end = count - 1;
while (end > 0) {
swap(a, 0, end);
end--;
siftdown(a, 0, end);
}
}
public void heapify(int[] a, int count) {
int start = (count - 2) / 2;
while (start >= 0) {
siftdown(a, start, count - 1);
start--;
}
}
public void siftdown(int[] a, int start, int end) {
int root = start;
while (root * 2 + 1 <= end) {
int child = root * 2 + 1;
int swap = root;
if (a[swap] < a[child]) {
swap = child;
}
if (child + 1 <= end && a[swap] < a[child + 1]) {
swap = child + 1;
}
if (root == swap) {
return;
} else {
swap(a, root, swap);
}
root = swap;
}
}
public void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
HeapSort 堆排序 基于伪代码实现的更多相关文章
- Heapsort 堆排序算法详解(Java实现)
Heapsort (堆排序)是最经典的排序算法之一,在google或者百度中搜一下可以搜到很多非常详细的解析.同样好的排序算法还有quicksort(快速排序)和merge sort(归并排序),选择 ...
- 说说 HeapSort 堆排序思想,以及个人优化方案。(老物)
听说你要排上亿个数据之 HeapSort ? 前言 : 来来来,今天我们来说说一个用来排大量数据所用的基础比较排序吧~ 注:阅读本文学习新技能的前置要求为:了解什么是二叉树及其数组性质,如果未达到要求 ...
- 算法分析之——heap-sort堆排序
堆排序是一种原地排序算法,不使用额外的数组空间,运行时间为O(nlgn).本篇文章我们来介绍一下堆排序的实现过程. 要了解堆排序.我们首先来了解一个概念,全然二叉树. 堆是一种全然二叉树或者近似全然二 ...
- 排序--HeapSort 堆排序
堆 排 序 堆排序.就是通过堆结构来排序.可以看之前写的http://www.cnblogs.com/robsann/p/7521812.html .关于堆的结构 堆排序先要使结构堆有序.所以要先使所 ...
- QuickSort 快速排序 基于伪代码实现
本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...
- 排序算法<No.5>【堆排序】
算法,是系统软件开发,甚至是搞软件的技术人士的核心竞争力,这一点,我坚信不疑.践行算法实践,已经有一段时间没有practise了,今天来一个相对麻烦点的,堆排序. 1. 什么是堆(Heap) 这里说的 ...
- 选择排序、快速排序、归并排序、堆排序、快速排序实现及Sort()函数使用
1.问题来源 在刷题是遇到字符串相关问题中使用 strcmp()函数. 在函数比较过程中有使用 排序函数 Sort(beg,end,comp),其中comp这一项理解不是很彻底. #include & ...
- Binary Heap(二叉堆) - 堆排序
这篇的主题主要是Heapsort(堆排序),下一篇ADT数据结构随笔再谈谈 - 优先队列(堆). 首先,我们先来了解一点与堆相关的东西.堆可以实现优先队列(Priority Queue),看到队列,我 ...
- Java实现---堆排序 Heap Sort
堆排序与快速排序,归并排序一样都是时间复杂度为O(N*logN)的几种常见排序方法.学习堆排序前,先讲解下什么是数据结构中的二叉堆. 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关 ...
随机推荐
- webService获取电话号归属地和获取天气情况步骤,及创建属于自己的webservice
一.什么是Web服务 Web服务是一种可以用来解决跨网络应用集成问题的开发模式,目的是保证不同平台的应用服务可以互操作 二.Web服务的三个核心 Soap: SOAP(Simple Object Ac ...
- VS 常用高效 快捷键
强迫智能感知:Ctrl+J.智能感知是Visual Studio最大的亮点之一,选择Visual Studio恐怕不会没有这个原因. 2 撤销:Ctrl+Z.除非你是天才,那么这个快捷键也是最常用的. ...
- HTML5入门(CSS样式-------------------(CSS基础知识点----------------------------))
CSS继承性+层叠性+盒子+浮动 一.CSS继承性 eg: <style> div{ ...
- ASP.NET MVC的运行机制--url的全局分析
全局 首先我们来看一副图片 首先,用户通过Web浏览器向服务器发送一条url请求,这里请求的url不再是xxx.aspx格式,而是http://HostName/ControllerNam ...
- ReactNative新手学习之路01-创建项目开始
新手学习之路01-创建项目开始 小菜鸟准备学习RN开发,决定写下自己的学习历程,方便其他也想要学习RN的人,后期会持续更新写下自己所有学习经历,一步步从菜鸟成长成业内高手.开发环境准备,本文默认环境已 ...
- js的this和面向对象编程
很奇怪的是很多书或资料没有把这个事情讲清楚. 关键就是在于没有一个整体的思维技术模式,问题被隔离了所以反而不容易理解. 我们先看this,这是js的关键字,指示函数的上下文对象. 这里问题就来了,比如 ...
- __delattr__\__delitem__
class Foo: def __init__(self,name): self.name=name def __getitem__(self, item): print(self.__dict__[ ...
- url 中 # ? & 的作用
1. # 10年9月,twitter改版.一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为http://twitter.com/username改版后,就变 ...
- 麦软社区Mindmanager现金抵用券使用流程
1.用户登录麦软社区:输入用户名密码 2.点击右上角发表话题,在麦软社区发表文章.教程.模板等等 3.填写要发布的内容 4.发布成功,等待审核 5.审核通过 6.审核通过的用户将会收到站内信,包含mi ...
- Opencv 完美配置攻略(Win8.1 + Opencv 2.4.8 + VS 2013)