Provide a generic Java class named SortedPriorityQueue that implements a

Provide a generic java class named

This preview shows page 5 - 9 out of 9 pages.

3. 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. & 4. Consider the following sorting algorithm that uses the class you wrote in the previous problem:
Image of page 5
Image of page 6
Image of page 7
Image of page 8
CMSC 451 Homework 3{Type x = q.remove(q.size()-1);returnx;}//Use the given algorithm to//sort an integer array.staticvoidsort(int[] array) {SortedPriorityQueue<Integer> queue = newSortedPriorityQueue<Integer>();for(inti = 0; i < array.length; i++)queue.add(array[i]);for(inti = 0; i < array.length; i++)array[i] = queue.remove();}//Define the required main() method.publicstaticvoidmain(String[] args) {intarray[] = {5,3,1,2,3,5,9};System.out.println("The array before sorting is as follows: ");for(inti=0;i<array.length;i++){System.out.print(array[i]+" ");}System.out.println();sort(array);System.out.println("The array after sorting is as follows: ");for(inti=0;i<array.length;i++){System.out.print(array[i]+" ");}System.out.println();}}
Image of page 9

You've reached the end of your free preview.

Want to read all 9 pages?

  • Fall '15
  • Darc
  • Array, int array

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes