Docsity

Algorithmic Asymptotic Complexity Exaples, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
Code samples on asymptotic complexity
For each of the following C++ code segments using a for loop, you will need to be able to evaluate the
asymptotic complexity, we can assume that each cout statement will have a constant run time, i.e. O(1)
1. for (int i = 0; i < n; i ++)
cout << " H e ll o Wo rl d " << endl;
Solution:
Pn−1
i=0 1 = O(n)
2. for (int i = 0; i < n * n; i ++)
cout << " H e ll o Wo rl d " << endl;
Solution:
Pn2
−1
i=0 1 = O(n2)
3. for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
cout << " H e ll o Wo rl d " << endl;
Solution:
Pn−1
i=0 Pn−1
j=0 1=Pn−1
i=0 n=O(n2)
4. for (int i = 0; i < n; i ++)
for (int j = 0; j < n * n; j ++)
cout << " H e ll o Wo rl d " << endl;
Solution:
Pn−1
i=0 Pn2
j=0 1=Pn−1
i=0 n2=O(n3)
5. for (int i = 0; i < n; i ++)
for (int j = 0; j < i; j ++)
cout << " H e ll o Wo rl d " << endl;
Solution: Uses Gauss’s formula Pn
i=1 i=n(n+1)
2
Pn−1
i=0 Pi
j=0 1=Pn−1
i=0 i=(n−1)(n−1+1)
2=n2
−n
2=O(n2)
6. for (int i = n; i > 0; i = i / 2)
cout << " H e ll o Wo rl d " << endl;
Solution:
This is literally the definition of log2nso O(log n)
1
pf2

Partial preview of the text

Download Algorithmic Asymptotic Complexity Exaples and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Code samples on asymptotic complexity

For each of the following C++ code segments using a for loop, you will need to be able to evaluate the asymptotic complexity, we can assume that each cout statement will have a constant run time, i.e. O(1)

  1. for ( int i = 0; i < n ; i ++) cout << " Hello World " << endl ;

Solution: ∑n− 1 i=0 1 =^ O(n)

  1. for ( int i = 0; i < n * n ; i ++) cout << " Hello World " << endl ;

Solution: ∑n^2 − 1 i=0 1 =^ O(n

  1. for ( int i = 0; i < n ; i ++) for ( int j = 0; j < n ; j ++) cout << " Hello World " << endl ;

Solution: ∑n− 1 i=

( ∑n− 1 j=0 1

∑n− 1 i=0 n^ =^ O(n

  1. for ( int i = 0; i < n ; i ++) for ( int j = 0; j < n * n ; j ++) cout << " Hello World " << endl ;

Solution: ∑n− 1 i=

( ∑n 2 j=0 1

∑n− 1 i=0 n

(^2) = O(n (^3) )

  1. for ( int i = 0; i < n ; i ++) for ( int j = 0; j < i ; j ++) cout << " Hello World " << endl ;

Solution: Uses Gauss’s formula

∑n i=1 i^ =^

n(n+1) 2 ∑n− 1 i=

( ∑i j=0 1

∑n− 1 i=0 i^ =^

(n−1)(n−1+1) 2 =^

n^2 −n 2 =^ O(n

  1. for ( int i = n ; i > 0; i = i / 2) cout << " Hello World " << endl ;

Solution: This is literally the definition of log 2 n so O(log n)

  1. for ( int i = n * n ; i > 0; i = i / 2 ) cout << " Hello World " << endl ;

Solution: This is the definition of log 2 n^2 = 2 log n = O(log n)

  1. for ( int i = 0; < n ; i ++) for ( int j = 1; j < n ; j = 2 * j ) cout << " Hello World " << endl ;

Solution: The inner loop runs O(log n) so

∑n− 1 i=0 log^ n^ =^ O(n^ log^ n)

  1. for ( int i = 0; i < n ; i ++) for ( int j = 1; j < i ; j = 2 * j ) cout << " Hello World " << endl ;

Solution: log 2 1 + log 2 2 + log 2 3 + .. + log 2 (n − 1) = log(n!) =

∑n ∫ (^) n i=1^ log^2 n^ = 1 ln^ xdx^ =^ n^ ln^ n^ −^ n^ −^ (ln 1^ −^ 1) =^ O(n^ log^ n)

  1. for ( int i = n ; i > 0; i = i / 2) for ( int j = 0; j < n ; j ++) cout << " Hello World " << endl ;

Solution: ∑log 2 n i=

( ∑n− 1 i=0 1

∑log 2 n i=0 n^ =^ O(n^ log^ n)

  1. for ( int i = n ; i > 0; i = i / 2) for ( int j = 0; j < i ; j ++) cout << " Hello World " << endl ;

Solution: Geometric Series n + n 2 + n 4 + n 8 + ... + 0 ≈ 2 n = O(n)