[英]How do Java Priority Queues Work Under The Hood?
I cannot seem to find any information to answer my question.我似乎找不到任何信息来回答我的问题。 So Java PriorityQueue are built using heaps.
所以 Java PriorityQueue是使用堆构建的。 Heaps have an insertion and deletion time of O(logn) so if I were to do a heap sort that would be O(nlogn).
堆的插入和删除时间为 O(logn),因此如果我要进行堆排序,则为 O(nlogn)。
But during creation of a heap it only takes O(n) time.但是在创建堆的过程中只需要 O(n) 时间。 So lets say I put the line
所以让我们说我把这条线
PriorityQueue<Character> heap = new PriorityQueue<>(list);
Is Java building the data structure with the values so its O(n) or is it inserting afterwards so its O(nlogn)? Java 是用值构建数据结构,所以它的 O(n) 还是它之后插入,所以它的 O(nlogn)?
You can always check the source: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/PriorityQueue.java您可以随时查看源: http : //hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/PriorityQueue.java
When you initialize it from an unsorted list, it uses the O(n) heapify
method.当你从一个未排序的列表初始化它时,它使用 O(n)
heapify
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.