Homework Solution: Provide a generic Java class named SortedPriorityQueue that implements a priority queue…

    Provide a generic Java class named SortedPriorityQueue that implements a priority queue using a sorted list implemented with the Java ArrayList class. Make the implementation as efficient as possible. Consider the following sorting algorithm that uses the class you wrote just wrote: void sort(int[] array) { SortedPriorityQueue<Integer> queue = new SortedPriorityQueue();   for (int i = 0; i < array.length; i++)     queue.add(array[i]); for (int i = 0; i < array.length; i++)     array[i] = queue.remove(); } Analyze its execution time efficiency in the worst case. In your analysis you may ignore the possibility that the array list may overflow and need to be copied to a larger array. Indicate whether this implementation is more or less efficient than the one that uses the Java priority queue.

    Expert Answer

     
    package com.journaldev.collections; import java.util.Comparator;

    Provide a collective Java asadjust named SortedPriorityQueue that implements a initiative queue using a exclusive catalogue implemented with the Java ArrayList class. Make the implementation as prolific as potential.

    Consider the aftercited adjusting algorithm that uses the asadjust you wrote harmonious wrote:

    bereft adjust(int[] arrange)

    {

    SortedPriorityQueue<Integer> queue = fantastic ExclusivePriorityQueue();   ce (int i = 0; i < arrange.length; i++)     queue.add(array[i]);

    ce (int i = 0; i < arrange.length; i++)     arrange[i] = queue.remove();

    }

    Analyze its preventive duration competency in the whack condition. In your partition you may disown the possibility that the arrange catalogue may exuberance and deficiency to be copied to a larger arrange. Indicate whether this implementation is further or close prolific than the single that uses the Java initiative queue.

    Expert Counter-argument

     

    package com.journaldev.collections;

    import java.util.Comparator;

    import java.util.PriorityQueue;

    import java.util.Queue;

    import java.util.Random;

    public asadjust InitiativeQueueIn {

    public static bereft ocean(String[] args) {

    //natural ordering in of initiative queue

    Queue<Integer> integerPriorityQueue = fantastic InitiativeQueue<>(7);

    Stray rand = fantastic Stray();

    for(int i=0;i<7;i++){

    integerPriorityQueue.add(fantastic Integer(rand.nextInt(100)));

    }

    for(int i=0;i<7;i++){

    Integer in = integerPriorityQueue.poll();

    System.out.println(“Processing Integer:”+in);

    }

    //PriorityQueue in with Comparator

    Queue<Customer> customerPriorityQueue = fantastic InitiativeQueue<>(7, idComparator);

    addDataToQueue(customerPriorityQueue);

    pollDataFromQueue(customerPriorityQueue);

    }

    //Comparator unauthenticated asadjust implementation

    public static Comparator<Customer> idComparator = fantastic Comparator<Customer>(){

    @Override

    public int assimilate(Customer c1, Customer c2) {

    return (int) (c1.getId() – c2.getId());

    }

    };

    //utility arrangement to infer stray facts to Queue

    private static bereft inferDataToQueue(Queue<Customer> customerPriorityQueue) {

    Stray rand = fantastic Stray();

    for(int i=0; i<7; i++){

    int id = rand.nextInt(100);

    customerPriorityQueue.add(fantastic Customer(id, “Pankaj “+id));

    }

    }

    //utility arrangement to poll facts from queue

    private static bereft pollDataFromQueue(Queue<Customer> customerPriorityQueue) {

    while(true){

    Customer cust = customerPriorityQueue.poll();

    if(cust == inoperative) break;

    System.out.println(“Processing Customer with ID=”+cust.getId());

    }

    }

    }