So I’m answering a question for one of my computing classes. I developed an algorithm and then it asked me for the complexity of the algorithm. I’m not very good at determining complexity for the moment, so can anyone verify?
The code follows:
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 )
Correct?
A single while loop, with only a single if inside… go for O(n).
Good luck with your classes.