your friend is wrong, it's not O(n^2). Try counting the number of cycles in the inner loop given an arbitrary "count" value; then, sum the result from 1 to n; you'll get ...
Looks to me like the inner loop wont run for the first time through the segment because count2 is equal to count. After count is incremented once that inner loop will have to run one more time every time through. So it cant just be O(n) right?
Last edited by quixomatic; October 20th, 2012 at 12:58 PM.
The complexity of the inner loop is O(2^n) then? Each time it multiplies count2 by 2 ...this stuff is driving me crazy I just cant wrap my head around it.
Then the complexity of the outer loop would just be O(n)?
If that were true
...
Clearly, your statement must be wrong.
Appreciate the help but I figured it out just looking at the patterns of the code and determined the outer loop is O(N) and that the inner loop just performs 2^n like I said earlier, which in log base 2 is just logN.
Since the while loop is inside of outer loop I multiplied O(N) by O(logN) giving me O(NlogN)
sorry, it's still wrong. Again, count the number of times the inner loop cycles ( no, it's not 1 nor 2^n. try following the code as it runs, eventually with a debugger or just with pen and paper ).
sorry, it's still wrong. Again, count the number of times the inner loop cycles ( no, it's not 1 nor 2^n. try following the code as it runs, eventually with a debugger or just with pen and paper ).
You know what, I really dont care...I worked on this for a huge chunk of yesterday and it was only a tiny fraction of my assignment. Not only that I was trying to be nice saying I appreciate the help, but what help have you given? Day and a half later and still not even a single post on what direction I should take other than "how many times was count = count * 2 invoked. I've asked multiple people who know how to find big-oh and they all agree its nlogn.
the inner loop just performs 2^n like I said earlier, which in log base 2 is just logN.
You did not say that. Rather, you asked if "the complexity of the inner loop is O(2^n)". It isn't, as you yourself have now concluded. What you probably have in mind is the idea that the value of count2 increases at a rate proportional to 2^n. Thus, the number of iterations of the inner loop is proportional to log n.
Originally Posted by quixomatic
Since the while loop is inside of outer loop I multiplied O(N) by O(logN) giving me O(NlogN)
Yes, that is correct
EDIT:
Originally Posted by quixomatic
Day and a half later and still not even a single post on what direction I should take other than "how many times was count = count * 2 invoked.
The thing is, figuring that out is the key to figuring out the complexity of the inner loop, and is likely to be the core lesson of this exercise. Once you know the complexity of the inner loop, the overall complexity of the code snippet is obvious since the outer loop's complexity is obvious.
Last edited by laserlight; October 21st, 2012 at 12:03 PM.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Appreciate the response Laserlight, I admit I did not have an strong understanding of what complexity is or how to find it, but when I said it performs 2^n I did mean that the inner function itself was computing exactly that. I may have reached my answer differently, but never once did superbonzo say I was getting closer or that I had the right idea.
Bookmarks