java - Is this method O(log N) or constant time? -


i have method inserts elements priority queue. want know performance time has.

i believe can o(1) if element being inserted can place @ bottom of queue. however, runs on o(log n) time, if element inserted new minimum , percolated way root. is reasoning correct?

here method insertion method:

/**  * insert priority queue, maintaining heap order.  * duplicates allowed.  * @param x item insert.  */ public void insert( anytype x ) {     if( currentsize == array.length - 1 )         enlargearray( array.length * 2 + 1 );          // percolate     int hole = ++currentsize;     for( array[ 0 ] = x; x.compareto( array[ hole / 2 ] ) < 0; hole /= 2 )         array[ hole ] = array[ hole / 2 ];     array[ hole ] = x; } 

i "no" in answer question "is reasoning correct?" typically o() notation taken indication of worst-case complexity of algorithm. can used average-case complexity, best-case complexity. (see here example when might use it.) have argued algorithm o(1) in best-case circumstances, not o(1) in average or worst-case circumstances.

leaving aside concern complexity of enlargearray() function (which may make more o(log n) although amortized time not, on other hand not part of "algorithm proper" anyway because pre-allocate array "big enough"), insertion algorithm o(log n) because both average , worst case complexity.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -