So, I answer the question of one of my computational classes. I developed an algorithm, and then asked me about the complexity of the algorithm. I'm not very good at complexity at the moment, so can anyone check?
The following code:
if( A.type is not Comparable ): return False // Max runs = 1 current β A.head // Max runs = 1 printedFirst β False // Max runs = 1 while( current.hasNext ): // Max runs = s-1 if ( current.value < current.next.value ): // Max runs = s-1 if ( printedFirst ): print ", " // Max runs = s-1 print "(" + current.value + ", " + current.next.value + ")" //runs = s-1 printedFirst β True // Max runs = s-1 current = current.next // Max runs = s-1
So we have
3( 1 ) + 6(s - 1) = 3 + 6s - 6 = 6s - 3 = O( n )
Right?
source share