java - How to insert values in descending order into a LinkedList in O(n log n) complexity? -
i have implement custom properyqueue , decided use linkedlist container values. order in inserted high value - low priority. therefore queue has values in descending order , priority ascending element' values smaller. how implement insertion method use complexity of o(n log n) ?
this priority queue :
public class priorityqueue<e extends comparable<? super e>> implements comparable { private linkedlist<e> queue; private int size; public priorityqueue(int size) { this.size = size; queue = new linkedlist<>(); } public priorityqueue() { this(50000); } public void insert(e value) { if (queue.size() == size) { try { throw new sizelimitexceededexception(); } catch (sizelimitexceededexception e) { e.printstacktrace(); } } if (value == null) { throw new nullpointerexception(); } else { queue.add(value); size--; } collections.sort(queue); collections.reverse(queue); } }
the complexity of insertion method o(n pow(n)) how can improve , algorithm should use ?
why don't use treeset
?
compareto
method of individual elements should first compare priority (returning negative value higher priority) , compare on other attributes in order make them unique.
Comments
Post a Comment