Friday, July 31, 2009

What is the Big-Oh efficiency of two nested for-loops?

If the order of the outer loop is A and of the inner loop is B, then the total efficiency is O(AB)

What is the Big-Oh efficiency of two nested for-loops?
I don't know much about it, but maybe this website can help.


http://msdn.microsoft.com/library/defaul...
Reply:O(n^2)
Reply:Assuming for code is as the following:





for i-%26gt; 1 to n {


for j-%26gt; 1 to n { }


}





For the first for-loop will be executed n times. For each of the statement of the outer loop, there will be n times execution of the inner loop.





Hence, the big-oh of the following code is O(n^2)
Reply:Assuming that the loop is:





for (int a = 0; a %26lt; 5; a++) {


for (int b = 0; b %26lt; a; b++) {


}


}





The O should be n^2. This is because the value of b is depending on a. Which means a is being used twice.



neutral skin tone

No comments:

Post a Comment