Search code examples
c++big-ocomplexity-theory

Total running time calculation using Big-O Asymptotic analysis


int f1(int n)
{ 
    int sum = 73; 
    for (int i=0; i < n; i++)
    { 
        for (int j=i; j >= 5; j/2)
        {
            sum--;
        }
    }
}

I am calculating the Total Running time of this algorithm determining the cost of each statement. I have got so far is this:

for(int i=0; i < n; i++) // runs in n time
for(int j=i; j >= 5; j/2) // runs in T(n/2) time
sum-- // runs nc/2 times (because it lies in inner loop which is running n/2 times)

Now, I have to find T(n) but I am not getting whether to do this n*T(n/2)) or n+ T(n/2). When T(n/2) solved by substitution it gives, O(log n) then would be the big-o for this algorithm O(n^2) or O(n log n)?


Solution

  • I think the Big-O will be O(n*Log2(n))

    for(int i = 0; i < n; ++i) // n
        for(int j = i; j >= 5; j /= 2) //log2(n) Because j is divided by 2 each step
            sum--; // 1