Beruflich Dokumente
Kultur Dokumente
Alg1(n)
For i = 1 to n
For j = 1 to 2!
Print(j)
Alg2(n)
For i = 1 to n
For j = 1 to 2!
Print(j)
Solution:
For the first algorithm (Alg1), each iteration of its inner loop takes constant time, and the
number of iterations of inner loop is 2 n regardless of i . Then, the total time is the above
runtime repeats n times in outer loop.
! !! !
For the second algorithm (Alg2), each iteration of its inner loop takes constant time but
the number of iterations of inner loop is 2! . Then, each iteration of the outer loop varies.
! !! !
2!!! − 1
𝑇!"#! 𝑛 = 1= 2! = − 1 = 2!!! − 2 = Ο(2! )
2−1
!!! !!! !!!