Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of this code

I am working on some homework so I cannot post any code. I am working on some code and at this point I have something like this (instead of functions I wrote time complexity):

while(O(n^2)) {
    O(n^4);
    O(n^2);
}

I estimated O's according to nested for-loops I have in functions. My question is what is actually time complexity of this whole thing? I wouldn't mind short explaination either. Thank you!

like image 316
CAPS LOCK Avatar asked Jan 18 '26 00:01

CAPS LOCK


2 Answers

EDIT: My previous answer was wrong, because I have made a mistake.

Now I understand, that your code can be refactored to

while(run == true)
{
    run = O(n^2);
    O(n^4);
    O(n^2);
}

so each iteration has O(n^4) complexity, because we have a polynomial n^4 + 2*(n^2) and we ditch the lower degree. Now you have to multiply it by number of iteration. For example if you get n iteration you end up with O(n^5). If you always have 1000000000 iterations, you still have O(n^4).

An excellent explanation of Big-O notation is here: What is a plain English explanation of "Big O" notation?

like image 111
Darth Hunterix Avatar answered Jan 19 '26 14:01

Darth Hunterix


What are you specifically trying to find out? If you want to know how long your code takes to compute, and break this down into specific loops and nested loops then I'd suggest looking into the stopwatch class:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx

Also you can watch the times live using by writing the elapse time of the stopwatches to labels and then calling me.update()

like image 42
FraserOfSmeg Avatar answered Jan 19 '26 16:01

FraserOfSmeg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!