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 common Java tabulate named SortedPriorityQueue that implements a initiative queue using a arranged schedule implemented with the Java ArrayList class. Make the implementation as fertile as likely.

    Consider the subjoined charactering algorithm that uses the tabulate you wrote sound wrote:

    vacant character(int[] adorn)

    {

    SortedPriorityQueue<Integer> queue = novel ArrangedPriorityQueue();   control (int i = 0; i < adorn.length; i++)     queue.add(array[i]);

    control (int i = 0; i < adorn.length; i++)     adorn[i] = queue.remove();

    }

    Analyze its project term aptitude in the whack event. In your dissection you may repudiate the possibility that the adorn schedule may inundation and demand to be copied to a larger adorn. Indicate whether this implementation is past or near fertile than the individual that uses the Java initiative queue.

    Expert Acceptance

     

    package com.journaldev.collections;

    import java.util.Comparator;

    import java.util.PriorityQueue;

    import java.util.Queue;

    import java.util.Random;

    public tabulate InitiativeQueueCopy {

    public static vacant main(String[] args) {

    //natural ordering copy of initiative queue

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

    Stray rand = novel Stray();

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

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

    }

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

    Integer in = integerPriorityQueue.poll();

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

    }

    //PriorityQueue copy with Comparator

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

    addDataToQueue(customerPriorityQueue);

    pollDataFromQueue(customerPriorityQueue);

    }

    //Comparator authorless tabulate implementation

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

    @Override

    public int parallel(Customer c1, Customer c2) {

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

    }

    };

    //utility manner to infer stray axioms to Queue

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

    Stray rand = novel Stray();

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

    int id = rand.nextInt(100);

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

    }

    }

    //utility manner to poll axioms from queue

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

    while(true){

    Customer cust = customerPriorityQueue.poll();

    if(cust == trifling) break;

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

    }

    }

    }