How to calculate time complexity of the following piece of code??

assume n= 2^k

i=n;
while (i>=1)
{
    j=i;
    while(j <= n)
    {
       <body>> // Needs theta(1)
        j=2*j;
    }
    i=i/2;
}

Recommended Answers

All 2 Replies

In this thread I explain a similar question by example. It sounds like you need to describe the time complexity of this code in terms of k but I'll leave you to figure that out.

hi,

i can calculate time complexity of a for loop...
but m nt able to understand how do we count while loops......
the code works in a weird way...
j increses in powers of 2 and i decreases ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.