Reputation: 33
Whats the big-O notation of this code?
for( int i=1; i<2n; i++)
x=x+1;
My answer = O(2*n)
Is this correct?
Upvotes: 2
Views: 85
Reputation: 2649
Consider this an A
algorithm
for( int i=1; i<2*n; i++)
x=x+1;
Algorithm A’s run-time: T(n) = 2n-1
2n-1 -> 2n
2n -> n
So the algorithm A’s time complexity is O(n)
.
Upvotes: 3
Reputation: 1224
The big-o run time of this is O(2n) like you guessed but that is usually just simplified to O(n).
Upvotes: 0
Reputation: 180434
It is O(n). Big O is meant to describe the complexity of the application and in this case it is linear so it is O(n).
Upvotes: 0