Sie sind auf Seite 1von 1

Example: Consider the following programs:

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)

For each of these algorithms, compute the asymptotic runtime.

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.

! !! !

𝑇!"#! 𝑛 = 1= 2! = 𝑛2! = Ο(𝑛2! )


!!! !!! !!!

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
!!! !!! !!!

Das könnte Ihnen auch gefallen