Question: What is the time and space complexity of a Priority Queue in Java? I have seen some varying answers on this topic. Consider the following
What is the time and space complexity of a Priority Queue in Java?
I have seen some varying answers on this topic. Consider the following java code I wrote, which utilizes 2 methods to sort words (from a hash table) by frequency and by their lexical ordering. I have used the standard Java Priority Queue which uses a custom comparator to sort hash table values into the queue. The functions then print the values of the queue. The functions are:
public void freqSort() { wordQueue = new PriorityQueue<>(uniqueWordCount(), new CompareWordFreq()); wordQueue.addAll(wordMap.entrySet()); while (!wordQueue.isEmpty()) { System.out.println(wordQueue.poll()); } } //utility method used to print words in lexicographic order public void lexSort() { wordQueue = new PriorityQueue<>(uniqueWordCount(), new compareWordLex()); wordQueue.addAll(wordMap.entrySet()); while (!wordQueue.isEmpty()) { System.out.println(wordQueue.poll()); } } What would be the time and space complexity of these functions using big O notation? Thanks in advance.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
