I have a question about calculating Big O runtime for a series of loops that are nested in an outer for loop.
For instance:
for (50,000 times)
{
for (n times)
{
//Do something
}
for (n-2 times)
{
//Do something
}
for (n times)
{
//Do something
}
for (n-2 times)
{
//Do something
}
}
The outer loop is constant, so I think this is ignored. Is it really so easy to make the following calculation?
N + N-2 + N + N-2
2N + 2 (N-2)
4N - 4
O (4N - 4)
O (4N) - after removing the constant -4
Is it correct?
Thank.
source
share