算法分析——优先队列
2019-07-19 16:28
148 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Heykeel/article/details/96483551
[code]public class PriorityQueue { // 最小堆 public PriorityQueue(int max) { this.heap = new int[max + 2]; this.max = max + 1; } // 维护的堆数据 private int[] heap; // 优先队列的最大容量 private int max; // 队列指针 private int capacity = 0; // 插入数据 public void insert(int key) { this.heap[++this.capacity] = key; swim(capacity); if (capacity == this.max) { this.deleteMin(); } } // 删除最小值 public int deleteMin() { int min = this.heap[1]; this.heap[1] = this.heap[capacity]; this.heap[capacity--] = 0; sink(1); return min; } // 上浮 private void swim(int k) { while (k > 1) { if (less(this.heap[k], this.heap[k / 2])) { swap(k, k / 2, this.heap); } k /= 2; } } // 下沉 private void sink(int k) { while (2 * k < this.capacity) { int j = 2 * k; if (j < this.capacity && less(this.heap[j+1], this.heap[j])) { j++; } swap(k, j, this.heap); k = j; } } }
相关文章推荐
- 数据结构和算法分析java--优先队列(堆实现)
- Java内存回收之可达性分析算法
- OpenCV学习笔记(28)KAZE 算法原理与源码分析(二)非线性尺度空间构建
- 买书算法分析
- 杂记之算法分析
- 算法导论<1、课程简介与算法分析>
- 算法效率分析框架
- 第十二周算法分析与设计: Find Largest Value in Each Tree Row
- 一道有道实习生笔试算法题分析
- 算法分析与设计实验三 回溯法 24点问题 n皇后问题
- 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现
- 【计算机算法分析】动态规划法——最长公共子序列问题
- 《机器学习实战》第二章k-近邻算法代码分析
- [转][算法]链接分析算法之:HITS算法
- 有向图的几个算法分析总结
- 一步步学算法(算法分析)---6(Floyd算法)
- 算法中时间复杂度分析
- 【算法设计与分析】基本概念(有向图,无向图,重复边,自环,简单图,度,子图,连通分支,强连通分支,稀疏图,稠密图)
- 算法设计与分析课
- [PAL算法说明]SAP HANA PAL线性回归预测分析Linear Regression算法说明LRREGRESSION