I'm new to Big-O, so I need some advice. Let's say I have a choice of two algorithms: one with several for loops in a row or one with a thrice-nested loop. For example, one of them is similar to the following:
for(int i = 0; i < a.length; i++)
{
// do stuff
}
for(int i = 0; i < b.length; i++)
{
for(int j = 0; j < c.length; j++)
{
// do something with b and c
}
}
for(int i = 0; i < d.length; i++)
{
// do stuff
}
And the other is structured this way:
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < b.length; j++)
{
for(int k = 0; k < c.length; k++)
{
}
}
}
These examples may seem impractical, but I assume that I'm just trying to illustrate my real questions: what is the big notation for each method and will an algorithm with 3 nested loops always be less efficient than an algorithm with many double nested loops (but not 3)?
source
share