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(归并排序),选择 ...
随机推荐
- hdu 4021 n数码
好题,6666 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2652410.html 题意:给出一个board,上面有24个位置,其中2 ...
- ajax请求成功后打开新窗口地址
转自:http://www.cnblogs.com/linjiqin/p/3148205.html jQuery.ajax({ "type":"post&qu ...
- 在js里面使用php语言
- mysql ODBC 在64位下提示找不到odbc驱动问题
在64位机器上,如果你想要连接32位mysql ,一般会安装mysql connector/ODBC 64位,并在配置ODBC数据源测试中连接正常,但在程序连接,如ASP.asp.net.VB.Del ...
- DOM--1 遵循最佳实践
为重用命名空间而进行规划 (function() { function $(id) { return document.getElementById(id); } function alertNode ...
- 简单几何(凸包) POJ 2187 Beauty Contest
题目传送门 题意:求两点的距离平方的最大值 分析:凸包模板题 /************************************************ * Author :Running_T ...
- POJ1850 Code(组合+康托展开)
题目问一个合法字符串的字典序是第几个,合法的字符串是指里面的字符严格递增. 先判断合不合法,然后用类似康托展开的过程去求.大概过程就是用组合数算出某长度某前缀有几个,累加起来. 真难一遍写对.. #i ...
- TextBlock 格式化内容
Text="{Binding ContentName,StringFormat=分享好书\{0\}}"
- HttpClient_HttpClient 对 cookie的处理
session的保持是通过cookie来维持的,所以如果用户有勾选X天内免登录,这个session 就X天内一直有效,就是通过这个cookie来维护.如果没选X天内免登录,基本上就本次才能保持sess ...
- 实例讲解Linux下的makefile
1.程序代码结构如下 makefile/ |-- Makefile |-- haha.c `-- hehe.c 1.1.需要被编译的源代码如下 $ cat haha.c #include " ...