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的更多相关文章

  1. 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 ...

  2. HeapSort 堆排序 基于伪代码实现

    此文原创, http://www.cnblogs.com/baokang/p/4735431.html ,禁止转载 GIF 动态图 伪代码 /* From Wikipedia, the free en ...

  3. 堆排序 Heapsort

    Prime + Heap 简直神了 时间优化好多,顺便就把Heapsort给撸了一发 具体看图 Heapsort利用完全二叉树+大(小)顶锥的结构每次将锥定元素和锥最末尾的元素交换 同时大(小)顶锥元 ...

  4. HeapSort自己yy-未完成

    #include <iostream> #include <cstdio> using namespace std; ; int a[maxn]; int HeapSize; ...

  5. [转载]堆排序(HeapSort) Java实现

    堆排序的思想是利用数据结构--堆.具体的实现细节: 1. 构建一个最大堆.对于给定的包含有n个元素的数组A[n],构建一个最大堆(最大堆的特性是,某个节点的值最多和其父节点的值一样大.这样,堆中的最大 ...

  6. 排序算法FOUR:堆排序HeapSort

    /** *堆排序思路:O(nlogn) * 用最大堆,传入一个数组,先用数组建堆,维护堆的性质 * 再把第一个数与堆最后一个数调换,因为第一个数是最大的 * 把堆的大小减小一 * 再 在堆的大小上维护 ...

  7. 算法导论学习-heapsort

    heap的定义:如果数组a[1,....n]满足:a[i]>a[2*i] && a[i]>a[2*i+1],1<=i<=n/2,那么就是一个heap,而且是ma ...

  8. 排序算法——QuickSort、MergeSort、HeapSort(C++实现)

    快速排序QuickSort template <class Item> void quickSort (Item a[], int l, int r) { if (r<=l) ret ...

  9. Heapsort 堆排序算法详解(Java实现)

    Heapsort (堆排序)是最经典的排序算法之一,在google或者百度中搜一下可以搜到很多非常详细的解析.同样好的排序算法还有quicksort(快速排序)和merge sort(归并排序),选择 ...

随机推荐

  1. C# 工程中引用出现感叹号

    问题:在工程中引用出现感叹号 原因1:  这是由于之前引用的Dll文件不见了. 右键有感叹号的项,然后选择 “属性” 里边有一个路径属性 这个路径就是之前这个Dll文件的路径,现在这个文件不在了,你需 ...

  2. 自动、手动同步FishEye, JIRA的联系人信息

    背景:在将FishEye和JIRA配置成共用用户信息,并且可以在二者之间自由切换,此时FishEye里的用户信息是不能更改的,只有更新了JIRA,然后让其同步至FishEye才行,如何进行设置呢? 答 ...

  3. XTU 1242 Yada Number 容斥

    Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime ...

  4. android 自定义弹出框AlertDialog ,很炫的哦

      于是就小小的模仿了下自己写了这个这样的效果,主要代码如下:dlg = new AlertDialog.Builder(context).create();dlg.show();dlg.getWin ...

  5. 由文章缩略图读出banner图

    <div class="banner"> {dede:sql sql="select litpic FROM #@__archives where typei ...

  6. 简单几何(四边形形状) UVA 11800 Determine the Shape

    题目传送门 题意:给了四个点,判断能构成什么图形,有优先规则 分析:正方形和矩形按照点积为0和长度判断,菱形和平行四边形按向量相等和长度判断,梯形按照叉积为0判平行.因为四个点是任意给出的,首先要进行 ...

  7. ZOJ2588 Burning Bridges(割边模板)

    题目要输出一个无向图的所有割边.用Tarjan算法: 一遍DFS,构造出一颗深度优先生成树,在原无向图中边分成了两种:树边(生成树上的边)和反祖边(非生成树上的边). 顺便求出每个结点的DFS序dfn ...

  8. POJ 1947 (树形DP+背包)

    题目链接: http://poj.org/problem?id=1947 题目大意:树中各点都由一条边连接.问要弄出个含有m个点的(子)树,至少需要截去多少条边. 解题思路: 设dp[i][j]为i总 ...

  9. android 常用第三方包的代码混淆

    首先在:project.properties 文件下,启动代码混淆 proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:pro ...

  10. TYVJ P1067 合唱队形 Label:上升子序列?

    背景 NOIP2004 提高组 第三道 描述     N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号 ...