heapsort
Introduction to Algorithms Third Edition
The (binary) heap data structure is an array object that we can view as a
nearly complete binary tree (see Section B.5.3), as shown in Figure 6.1. Each
node of the tree corresponds to an element of the array. The tree is com-
pletely filled on all levels except possibly the lowest, which is filled from the
left up to a point. An array A that represents a heap is an object with two at-
tributes: A: length, which (as usual) gives the number of elements in the array, and
A: heap-size, which represents how many elements in the heap are stored within
array A. That is, although AŒ1 : : A: length may contain numbers, only the ele-
ments in AŒ1 : : A: heap-size, where 0 Ä A: heap-size Ä A: length, are valid ele-
ments of the heap. The root of the tree is AŒ1, and given the index i of a node, we
can easily compute the indices of its parent, left child, and right child:
indiex indices 下标
For the heapsort algorithm, we use max-heaps. Min-heaps commonly imple-
ment priority queues, which we discuss in Section 6.5.
最小堆通常用于构造优先队列。
the largest element in a max-heap is stored at the root, and the subtree rooted at a node contains6.1 Heaps 153
values no larger than that contained at the node itself. A min-heap is organized in the opposite way.
a max-heap
A[PARENT(i)]>=A[i] a min-heap
A[PARENT(i)]<=A[i]
(上根下枝)
heapsort的更多相关文章
- Java演算法之堆排序(HeapSort)
import java.util.Arrays; publicclass HeapSort { inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,9 ...
- HeapSort 堆排序 基于伪代码实现
此文原创, http://www.cnblogs.com/baokang/p/4735431.html ,禁止转载 GIF 动态图 伪代码 /* From Wikipedia, the free en ...
- 堆排序 Heapsort
Prime + Heap 简直神了 时间优化好多,顺便就把Heapsort给撸了一发 具体看图 Heapsort利用完全二叉树+大(小)顶锥的结构每次将锥定元素和锥最末尾的元素交换 同时大(小)顶锥元 ...
- HeapSort自己yy-未完成
#include <iostream> #include <cstdio> using namespace std; ; int a[maxn]; int HeapSize; ...
- [转载]堆排序(HeapSort) Java实现
堆排序的思想是利用数据结构--堆.具体的实现细节: 1. 构建一个最大堆.对于给定的包含有n个元素的数组A[n],构建一个最大堆(最大堆的特性是,某个节点的值最多和其父节点的值一样大.这样,堆中的最大 ...
- 排序算法FOUR:堆排序HeapSort
/** *堆排序思路:O(nlogn) * 用最大堆,传入一个数组,先用数组建堆,维护堆的性质 * 再把第一个数与堆最后一个数调换,因为第一个数是最大的 * 把堆的大小减小一 * 再 在堆的大小上维护 ...
- 算法导论学习-heapsort
heap的定义:如果数组a[1,....n]满足:a[i]>a[2*i] && a[i]>a[2*i+1],1<=i<=n/2,那么就是一个heap,而且是ma ...
- 排序算法——QuickSort、MergeSort、HeapSort(C++实现)
快速排序QuickSort template <class Item> void quickSort (Item a[], int l, int r) { if (r<=l) ret ...
- Heapsort 堆排序算法详解(Java实现)
Heapsort (堆排序)是最经典的排序算法之一,在google或者百度中搜一下可以搜到很多非常详细的解析.同样好的排序算法还有quicksort(快速排序)和merge sort(归并排序),选择 ...
随机推荐
- Java Hour 13 集合基础
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. Java 中的集 ...
- CF 149D Coloring Brackets 区间dp ****
给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内 ...
- 我对java反射机制的理解
我们平常怎么用一个使用类,怎么使用类的方法?其实就是创建一个对象,并且通过这个对象调用这个方法.不过这有一个问题,就是这个对象的载体就和这个对象产生了耦合,怎么降低两者间的耦合呢?java的反射机制就 ...
- Z-XML团队 软件工程课之我感我思我收获
<软件工程>这门课像我们的诤友,不断督促我们前进,又不断指引我们收获.时间飞逝,我们Z-XML团队一个个完成了课程中的所有任务,一步步走到了期末年末. 走的远了,也该回头看看.全员7人回顾 ...
- SQL Server 2008 数据库同步的两种方式 (发布、订阅)
参考转载: SQL Server 2008 数据库同步的两种方式 (发布.订阅) 使用Sqlserver事务发布实现数据同步
- loj 1018(状压dp+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25844 思路:首先预处理出点在同一直线上的所有的点集状态(dp[i ...
- Android之SurfaceView学习(一)
对应的中文翻译SurfaceView是视图(View)的继承类,这个视图里内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸.Surfaceview控制这个Surface ...
- new一个二维数组
.定义一个二维数组 char **array1 array1 = new char *[x]; for(i=0;i<x;++i) array1[i] = new char[y]; ...用的时候 ...
- 大牛之路II
时间限制:500MS 内存限制:1000K 提交次数:138 通过次数:31 题型: 编程题 语言: C++;C Description 要成为ACM大牛,要掌握很多必需的知识点.某些知识点可以 ...
- TYVJ P1083 分糖果 Label:bfs
描述 童年的我们,将和朋友分享美好的事物作为自己的快乐.这天,C小朋友得到了Plenty of candies,将要把这些糖果分给要好的朋友们.已知糖果从一个人传给另一个人需要1 秒的时间,同一个小朋 ...