The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Hello. I would like to get my program explained to me. You wrote the program for me. I just do not understand it. I read the instruction and do not know what the professor wants. All I understood was that he wants me to write a program implementing priority queue. The second paragraph of the instructions is what really confuses me. Help. Maybe some comments. I normally like to understand the program and add or change stuff to it before I turn it in to no get in trouble.
Write your own implementation of a priority queue of integers: a priority queue maintains its elements sorted. The operations that you need to provide are inserting an integer, removing an integer, checking if a given integer is already contained in the queue. Duplicate elements are permitted. Do not use any of the predefined collections from the Java libraries.
Â
Provide a test program that inserts random values into two priority queues (one instance of your implementation, and one instance of the java.util.PriorityQueue<Integer>). Measure the runtime performance for 10000, 20000, ... up to 100000 insertions.
Â
Deliverables are your class implementation and your test program, and a document describing your design and your results (no more than 2 pages).
Â
Â
Â
Â
/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*/import java.util.PriorityQueue;import java.util.Random;public class
MyPriorityQueue {int arr[];int capacity;int count;public MyPriorityQueue(int capacity) {this.capacity = capacity;this.arr=new int[capacity];count=0;}//this method will insert a new integer into queuepublic void insert(int n){if(count==capacity){System.out.println("Queue is full");}else{arr[count]=Integer.MIN_VALUE;increaseKey(count,n);count++;}}void increaseKey(int i,int n){arr[i]=n;while(i>=0&&arr[i/2]<arr[i]){int temp=arr[i/2];arr[i/2]=arr[i];arr[i]=temp;i=i/2;}}void maxHeapify(int i){int left = 2 * i + 1;int right = 2 * i + 2;int largest = i;if( left < capacity && arr[ left ] > arr[ largest ] )largest = left;if( right < capacity && arr[ right ] > arr[ largest ] )largest = right;if( largest != i ){int temp = arr[ i ];arr[ i ] = arr[ largest ];arr[ largest ] = temp;
import java.util.PriorityQueue;import java.util.Random;public class Test {public static void main(String[] args) {System.out.println("Number of insertMyPriorityQueueJava PriorityQueue");for(int i=1;i<=10;i++){int n=10000*i;MyPriorityQueue p=newMyPriorityQueue(n);PriorityQueue<Integer> javaP=new PriorityQueue<Integer>(n);Random r=new Random();long start=System.nanoTime();for(int j=0;j<n;j++){int num=r.nextInt();javaP.add(num);}long stop=System.nanoTime();long javaQueueTime=stop-start;start=System.nanoTime();for(int j=0;j<n;j++){int num=r.nextInt();p.insert(num);}stop=System.nanoTime();long myQueueTime=stop-start;System.out.printf("%-11d%20d%15d\n",n,myQueueTime,javaQueueTime);}}}
-----------