算法导论Java实现-优先级队列(6.5章节)

简介:


优先级队列,是堆数据结构的典型应用。优先级队列的一个典型应用,就是排队任务的有限调度,当一个任务结束后,优先执行当前优先级最高的任务。队列一个任务是,调用INSERT方法。

 
  1. package lhz.algorithm.chapter.six; 
  2. /** 
  3.  * “优先级队列”,《算法导论》6.5章节 
  4.  * 原文摘要: 
  5.  * A priority queue is a data structure for maintaining a set S of elements, each with an 
  6.  * associated value called a key. A max-priority queue supports the following operations. 
  7.  * • INSERT(S, x) inserts the element x into the set S. This operation could be written as S← S{x}. 
  8.  * • MAXIMUM(S) returns the element of S with the largest key. 
  9.  * • EXTRACT-MAX(S) removes and returns the element of S with the largest key. 
  10.  * • INCREASE-KEY(S, x, k) increases the value of element x's key to the new value k, 
  11.  * which is assumed to be at least as large as x's current key value. 
  12.  * 堆的一种实际应用,可用于任务调度队列等。包含四个操作方法。 
  13. * 原文地址: http://mushiqianmeng.blog.51cto.com/3970029/743611
  14.  * @author lihzh(苦逼coder) 
  15.  */ 
  16. public class PriorityQueue { 
  17.      
  18.     private final int DEFAULT_CAPACITY_VALUE = 16
  19.     //初始化一个长度为16的队列(可作为构造参数),此处选择16参考HashMap的初始化值 
  20.     private int capacity = DEFAULT_CAPACITY_VALUE; 
  21.     private int[] quene = new int[capacity]; 
  22.     //堆大小 
  23.     private int heapSize = 0
  24.  
  25.     /** 
  26.      * 返回当前最大值 
  27.      * @return 
  28.      */ 
  29.     public int maximum() { 
  30.         return quene[0]; 
  31.     } 
  32.      
  33.     /** 
  34.      * 往优先级队列出,插入一个元素 
  35.      * 利用INCREASE-Key方法,从堆的最后增加元素 
  36.      * 伪代码: 
  37.      * MAX-HEAP-INSERT(A, key) 
  38.      * 1 heap-size[A] ← heap-size[A] + 1 
  39.      * 2 A[heap-size[A]] ← -∞ 
  40.      * 3 HEAP-INCREASE-KEY(A, heap-size[A], key) 
  41. * 时间复杂度:O(lg n) 
  42.  
  43.      * @param value 待插入元素 
  44.      */ 
  45.     public void insert(int value) { 
  46.         //注意堆容量和数组索引的错位 1 
  47.         quene[heapSize] = value; 
  48.         heapSize++; 
  49.         increaceKey(heapSize, value); 
  50.     } 
  51.      
  52.     /** 
  53.      * 增加给定索引位元素的值,并重新构成MaxHeap。 
  54.      * 新值必须大于等于原有值 
  55.      * 伪代码: 
  56.      * HEAP-INCREASE-KEY(A, i, key) 
  57.      * 1 if key < A[i] 
  58.      * 2 then error "new key is smaller than current key" 
  59.      * 3 A[i] ← key 
  60.      * 4 while i > 1 and A[PARENT(i)] < A[i] 
  61.      * 5 do exchange A[i] ↔ A[PARENT(i)] 
  62.      * 6 i ← PARENT(i) 
  63.      * 时间复杂度:O(lg n) 
  64.      * @param index 索引位 
  65.      * @param newValue 新值 
  66.      */ 
  67.     public void increaceKey (int heapIndex, int newValue) { 
  68.         if (newValue < quene[heapIndex-1]) { 
  69.             System.err.println("错误:新值小于原有值!"); 
  70.             return
  71.         } 
  72.         quene[heapIndex-1] = newValue; 
  73.         int parentIndex = heapIndex / 2
  74.         while (parentIndex > 0 && quene[parentIndex-1] < newValue ) { 
  75.             int temp = quene[parentIndex-1]; 
  76.             quene[parentIndex-1] = newValue; 
  77.             quene[heapIndex-1] = temp; 
  78.             heapIndex = parentIndex; 
  79.             parentIndex = parentIndex / 2
  80.         } 
  81.     } 
  82.      
  83.     /** 
  84.      * 返回堆顶元素(最大值),并且将堆顶元素移除 
  85.      * 伪代码: 
  86.      * HEAP-EXTRACT-MAX(A) 
  87.      * 1 if heap-size[A] < 1 
  88.      * 2 then error "heap underflow" 
  89.      * 3 max ← A[1] 
  90.      * 4 A[1] ← A[heap-size[A]] 
  91.      * 5 heap-size[A] ← heap-size[A] - 1 
  92.      * 6 MAX-HEAPIFY(A, 1) 
  93.      * 7 return max 
  94.      * 时间复杂度:O(lg n), 
  95.      * @return 
  96.      */ 
  97.     public int extractMax() { 
  98.         if (heapSize < 1) { 
  99.             System.err.println("堆中已经没有元素!"); 
  100.             return -1
  101.         } 
  102.         int max = quene[0]; 
  103.         quene[0] = quene[heapSize-1]; 
  104.         heapSize--; 
  105.         maxHeapify(quene, 1); 
  106.         return max; 
  107.     } 
  108.      
  109.      
  110.     /** 
  111.      * 之前介绍的保持最大堆的算法 
  112.      * @param array 
  113.      * @param index 
  114.      */ 
  115.     private  void maxHeapify(int[] array, int index) { 
  116.         int l = index * 2
  117.         int r = l + 1
  118.         int largest; 
  119.         //如果左叶子节点索引小于堆大小,比较当前值和左叶子节点的值,取值大的索引值 
  120.         if (l <= heapSize && array[l-1] > array[index-1]) { 
  121.             largest = l; 
  122.         } else { 
  123.             largest = index; 
  124.         } 
  125.         //如果右叶子节点索引小于堆大小,比较右叶子节点和之前比较得出的较大值,取大的索引值 
  126.         if (r <= heapSize && array[r-1] > array[largest-1]) { 
  127.             largest = r; 
  128.         } 
  129.         //交换位置,并继续递归调用该方法调整位置。 
  130.         if (largest != index) { 
  131.             int temp = array[index-1]; 
  132.             array[index-1] = array[largest-1]; 
  133.             array[largest-1] = temp; 
  134.             maxHeapify(array, largest); 
  135.         } 
  136.     } 

 

简单的测试代码:

 
  1. public static void main(String[] args) { 
  2.         PriorityQueue q = new PriorityQueue(); 
  3.         q.insert(2); 
  4.         q.insert(6); 
  5.         q.insert(3); 
  6.         q.insert(8); 
  7.         q.insert(7); 
  8.         q.insert(9); 
  9.         q.insert(1); 
  10.         q.insert(10); 
  11.         q.insert(9); 
  12.         System.out.println(q.extractMax()); 
  13.         System.out.println(q.extractMax()); 
  14.         q.insert(9); 
  15.         q.insert(1); 
  16.         q.insert(10); 
  17.         System.out.println(q.extractMax()); 
  18.         System.out.println(q.extractMax()); 
  19.         System.out.println(q.extractMax()); 
  20.         System.out.println(q.extractMax()); 
  21.         System.out.println(q.extractMax()); 
  22.         System.out.println(q.extractMax()); 
  23.         System.out.println(q.extractMax()); 
  24.     } 

 


     本文转自mushiqianmeng 51CTO博客,原文链接:http://blog.51cto.com/mushiqianmeng/743611,如需转载请自行联系原作者




相关文章
|
4月前
|
负载均衡 算法 关系型数据库
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
本文聚焦 MySQL 集群架构中的负载均衡算法,阐述其重要性。详细介绍轮询、加权轮询、最少连接、加权最少连接、随机、源地址哈希等常用算法,分析各自优缺点及适用场景。并提供 Java 语言代码实现示例,助力直观理解。文章结构清晰,语言通俗易懂,对理解和应用负载均衡算法具有实用价值和参考价值。
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
|
4月前
|
人工智能 算法 NoSQL
LRU算法的Java实现
LRU(Least Recently Used)算法用于淘汰最近最少使用的数据,常应用于内存管理策略中。在Redis中,通过`maxmemory-policy`配置实现不同淘汰策略,如`allkeys-lru`和`volatile-lru`等,采用采样方式近似LRU以优化性能。Java中可通过`LinkedHashMap`轻松实现LRUCache,利用其`accessOrder`特性和`removeEldestEntry`方法完成缓存淘汰逻辑,代码简洁高效。
174 0
|
8月前
|
存储 算法 安全
探究‘公司禁用 U 盘’背后的哈希表算法与 Java 实现
在数字化办公时代,信息安全至关重要。许多公司采取“禁用U盘”策略,利用哈希表算法高效管理外接设备的接入权限。哈希表通过哈希函数将设备标识映射到数组索引,快速判断U盘是否授权。例如,公司预先将允许的U盘标识存入哈希表,新设备接入时迅速验证,未授权则禁止传输并报警。这有效防止恶意软件和数据泄露,保障企业信息安全。 代码示例展示了如何用Java实现简单的哈希表,模拟公司U盘管控场景。哈希表不仅用于设备管理,还在文件索引、用户权限等多方面助力信息安全防线的构建,为企业数字化进程保驾护航。
|
11月前
|
存储 人工智能 算法
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
这篇文章详细介绍了Dijkstra和Floyd算法,这两种算法分别用于解决单源和多源最短路径问题,并且提供了Java语言的实现代码。
551 3
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
|
11月前
|
算法 Java 数据中心
探讨面试常见问题雪花算法、时钟回拨问题,java中优雅的实现方式
【10月更文挑战第2天】在大数据量系统中,分布式ID生成是一个关键问题。为了保证在分布式环境下生成的ID唯一、有序且高效,业界提出了多种解决方案,其中雪花算法(Snowflake Algorithm)是一种广泛应用的分布式ID生成算法。本文将详细介绍雪花算法的原理、实现及其处理时钟回拨问题的方法,并提供Java代码示例。
833 2
|
算法 Java
LeetCode经典算法题:矩阵中省份数量经典题目+三角形最大周长java多种解法详解
LeetCode经典算法题:矩阵中省份数量经典题目+三角形最大周长java多种解法详解
143 6
|
搜索推荐 算法 Java
经典排序算法之-----选择排序(Java实现)
这篇文章通过Java代码示例详细解释了选择排序算法的实现过程,包括算法的基本思想、核心代码、辅助函数以及测试结果,展示了如何通过选择排序对数组进行升序排列。
经典排序算法之-----选择排序(Java实现)
|
存储 算法 Java
LeetCode经典算法题:打家劫舍java详解
LeetCode经典算法题:打家劫舍java详解
143 2
|
人工智能 算法 Java
LeetCode经典算法题:井字游戏+优势洗牌+Dota2参议院java解法
LeetCode经典算法题:井字游戏+优势洗牌+Dota2参议院java解法
143 1

热门文章

最新文章