What is the time complexity of building a PriorityQueue from a collection?

What is the complexity of the Java PriorityQueue constructor with Collection? I used the constructor:

PriorityQueue(Collection<? extends E> c)

Is complexity O (n) or O (n * log (n))?

+4
source share
2 answers

The time complexity for initializing a PriorityQueuefrom a collection, even unsorted, is O (n). Internally, it uses a procedure called siftDown()to "heapify" the array in place. (This is also called pushdown in the literature.)

. , - O (log n), n O (n log n). , . (, siftUp().)

- , , O (log n), "" siftDown() , , , . , n times log (n); n .

. , , .

+1

O(log(n)), n O(n).

PriorityQueue(Collection c) , SortedSet, a PriorityQueue Collection -. SortedSet PriorityQueue, O(n) ( ). Collection O(n log(n)).

+4

Source: https://habr.com/ru/post/1623546/


All Articles