algorithm ch6 priority queque
堆数据结构的一个重要用处就是:最为高效的优先级队列。优先级队列分为最大优先级队列和最小优先级队列,其中最大优先级队列的一个应用实在一台分时计算机上进行作业的调度。当用堆来实现优先级队列时,需要在队中的每个元素里存储对应的应用对象句柄(handle)。这里对象柄用数组下标表示,因为在堆操作中,堆元素会改变在数组中的位置,所以具体实现中为了重新定义堆元素,我们需要更新应用对象中的数组下标。简单的实现代码如下:
int HeapMaximum(int a[], int &heapSize)
{
return a[];
}
int HeapExtractMax(int a[], int &iHeapSize)
{
if(iHeapSize < )
{
cout << "heap underflow." << endl;
}
int iMax = a[];
a[] = a[iHeapSize];
iHeapSize = iHeapSize - ;
MaxHeapify(a, , iHeapSize);
return iMax;
}
void HeapIncreaseKey(int a[], int iPos, int iKey)
{
if(iKey < a[iPos])
{
cout << "new key is smaller than current key." << endl;
return;
}
a[iPos] = iKey;
while(iPos > && a[parent(iPos)] < a[iPos])
{
swap(a[iPos], a[parent(iPos)]);
iPos = parent(iPos);
}
}
void MaxHeapInsert(int a[], int iKey, int &iHeapSize)
{
iHeapSize += ;
a[iHeapSize] = -INT_MAX;
HeapIncreaseKey(a, iHeapSize, iKey);
}
同时还找到一份不错的学习代码http://www.cnblogs.com/Anker/archive/2013/01/23/2873951.html
好好学习,总会有收获。
algorithm ch6 priority queque的更多相关文章
- [Algorithm] Heap & Priority queue
这里只是简单的了解,具体内容详见推荐的原链接 注意堆和树的区别 堆就是优先级队列的实现形式 堆排序 排序过程 Ref: 排序算法之堆排序(Heapsort)解析 第一步(构造初始堆): {7, 5, ...
- algorithm ch6 heapsort
堆排序利用的是堆这种数据结构来对进行排序,(二叉)堆可以被视为一棵完全的二叉树,树的每个节点与数组中存放该节点的值得那个元素对应.这里使用最大堆进行排序算法设计,最大堆就是parent(i) > ...
- A Fast Priority Queue Implementation of the Dijkstra Shortest Path Algorithm
http://www.codeproject.com/Articles/24816/A-Fast-Priority-Queue-Implementation-of-the-Dijkst http:// ...
- Algorithm Part I:Priority Queues
1.binary heap实现 BinaryHeap.h #ifndef BINARYHEAP_H #define BINARYHEAP_H class BinaryHeap { public: Bi ...
- Thread Pool Engine, and Work-Stealing scheduling algorithm
http://pages.videotron.com/aminer/threadpool.htm http://pages.videotron.com/aminer/zip/threadpool.zi ...
- [Algorithm] 如何正确撸<算法导论>CLRS
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- Algorithm | Sort
Bubble sort Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algo ...
随机推荐
- YAGNI 声明
1.YAGNI介绍 YAGNI 全名是 You aren't Going to Need It,在你设计草案的初稿中,应该努力使用最简单可以工作的事物,直至程序的某个方面要求你添加额外的特性. 2.思 ...
- mybatis if标签比较字符串
项目中需要在mybatis后台比较字符串 因为mybatis映射文件使用的是ognl表达式,所以不能使用 <if test="type == '0'"> 解决: < ...
- oracle12c 新建表空间
第1步:创建临时表空间 create temporary tablespace jeeplus_temp tempfile 'D:\app\Administrator\virtual\product\ ...
- PHP实现字节数Byte转换为KB、MB、GB、TB
function getFilesize($num) { $p = 0; $format = 'bytes'; if( $num > 0 && $num < 1024 ) ...
- 利用devcon工具编写bat脚本一键控制系统设备,如开启关闭网卡
系统WIN7 x64位 下载devcon命令行工具 Download the "Windows Driver Kit (WDK) 7.1.0 from Microsoft: http://w ...
- Bellman_ford标准算法
Bellman_ford求最短路可以说这个算法在某些地方和dijkstra还是有些相似的,它们的松弛操作基本还是一样的只不过dijkstra以图中每个点为松弛点对其相连接的所有边进行松弛操作 而Bel ...
- 第四次JAVA作业
public class TvbDog { public static void main(String[] args) { Dog per=new Dog("陈狗"," ...
- javascript 数组以及对象的深拷贝
如果 let arr2 = arr1: 那么只是赋值的引用,改变arr2也会相应的改变arr1: 如果 let arr2 = [].concat(arr1): 如果arr1里面不是引用类型,那么ar ...
- can be found for element 'tx:annotation-driven'
错误描述: ERROR [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (ContextLoader.java:308) - Cont ...
- [luogu P1442] 铁球落地
题目链接 线段树\(+dp\). 先用线段树预处理出每个线段从左边和右边掉落到哪里,记为\(f[i][0/1]\). 然后记\(g[i][0/1]\)为到达第\(i\)个线段的左边或右边所要的最小时间 ...