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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -