If the following loop structure is under Analysis of Upper bound, does it still compute to O(n^2)? I'm confused since inner loop has a dependency on the outer loop and with each outer iteration, the inner for loop loops one less time. In addition to whatever O(n) is, will the time complexity function be "n!.n+C" where C is a constant? I'm assuming n! because of inner loop.
for(int i=n;i>0;i--)
{
for(int j=i;j>=1;j--)
{
count++;
}
}
n
, say n = 1,2,...,15 and printcount
, and you'll see the answer. Or simplify the inner loop tocount += i
and you'll get much closer to the answer. You may then also want to simplify the outer loop to something like1 + 2 + ... + n
, which is an arithmetic sequence, known to sum up ton*(n+1)/2
, which is the final formula for the two loops being simplified to the very end. – Kktpn^2
. Pretendn
is a million, and not something small like 10 or 20 to conceptualize this. Reduce the number of times by half on each iteration (like for binary search), then you're talking a different thing altogether. – Rufina