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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started