Question #203701

Calculate the time complexity of the following program fragments.

(a) š‘“š‘œš‘Ÿ (š‘– = 1; š‘– ≤ š‘›; š‘– āˆ— = 2)

{ š‘„=š‘„+1 }

(b) š‘“š‘œš‘Ÿ (š‘– = 1; š‘– ≤ š‘›; š‘– + +)

š‘“š‘œš‘Ÿ (š‘— = 1; š‘— ≤ š‘›; š‘— = š‘— āˆ— 2) {

....

....

}


Expert's answer

a)

It is executed, for i=1,2,4,8,...,2ni=1,2,4,8,...,2^n

The time complexity:

T(n)=⌊log2nāŒ‹T(n)=\lfloor log_2n\rfloor


b)

It is executed, n times for j=1,2,4,8,...,2nj=1,2,4,8,...,2^n

The time complexity:

T(n)=n⌊log2nāŒ‹T(n)=n\lfloor log_2n\rfloor

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS