Question #271978

Calculate the time complexity of the following program fragments.

(a) 𝑓𝑜𝑟 (𝑖 = 1; 𝑖 ≤ 𝑛; 𝑖 ∗ = 2)

{

𝑥 = 𝑥 + 1

}

(b) 𝑓𝑜𝑟 (𝑖 = 1; 𝑖 ≤ 𝑛; 𝑖 + +)

𝑓𝑜𝑟 (𝑗 = 1; 𝑗 ≤ 𝑛; 𝑗 = 𝑗 ∗ 2)

{

… .

… .

}


1
Expert's answer
2021-11-26T17:42:54-0500

a)

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

The time complexity:

T(n)=log2nT(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)=nlog2nT(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!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS