My code right now has an O (N) input time and an O (1) delete time. I need to change this. I am trying to implement O (1) input time and O (N) delete time.
Legend:
nItems = number of elements / objects. Initially set to 0.
queArray is my array of long integers.
Here are my two methods. The insert method performs all sorting. Delete method only one row - delete the first element in the array, which turned out to be the smallest number thanks to our Insert method.
If I had to change the insertion time to O (1), would I need to give a "sort task" to delete the method? This is a priority queue, and we must sort it, otherwise it will be just another queue with numbers in a random order.
Please any help would be enjoyable.
public void insert(long item) { int j; if(nItems==0) // if no items, queArray[nItems++] = item; // insert at 0 else { for(j=nItems-1; j>=0; j--) { // start at the end if( item > queArray[j] ) // if new item larger, queArray[j+1] = queArray[j]; // shift upward else // if smaller, break; // done shifting } // end for queArray[j+1] = item; // insert it nItems++; } // end else (nItems > 0) } public long remove() // remove minimum item { return queArray[--nItems]; }
source share