I have a question if I have my $k=300$ and my loop is like this :
for( int x = 0 ; x<n ; x--){
for(int y=0 ; y<k; y++){
...
}
}
Is this still $O(n^2)$? If no, why? Thank you :)
I have a question if I have my $k=300$ and my loop is like this :
for( int x = 0 ; x<n ; x--){
for(int y=0 ; y<k; y++){
...
}
}
Is this still $O(n^2)$? If no, why? Thank you :)
Assuming that you meant x++
rather than x--
, the complexity is $\Theta(nk)$. Since $k = 300$, you can simplify that to $\Theta(n)$. As Ran G. mentions, since $n = O(n^2)$, then your loop is also $O(n^2)$. It's also $O(2^n)$. But the tight analysis is $\Theta(n)$.
x--
then x
will be smaller then $n$ for a long time, until reach INT_MIN
and turn to INT_MAX
.
– Bartosz Przybylski
Feb 09 '13 at 08:26
INT_MAX
,INT_MIN
are both O(1) (say, $2^{64}$ ?), so the loop anyhow runs less than, say $O(($INT_MAX
$-$INT_MIN
$)^2)=O(1)$..
– Ran G.
Feb 09 '13 at 08:37