Quiz 1
NAME:
Code Snippet Runtimes
Find the runtimes of the following code snippets:
Algorithm 1 func1( int n )
1: i = 0, j = 0;
2: while ( i ≤ n ) do
3:
i + +;
4: end while
5: while ( j ≤ n ) do
6:
j + +;
7: end while
What is the runtime of f unc1?
Algorithm 2 func2( int n )
1: k = n;
2: while ( k > 0 ) do
3:
i = 0;
4:
while ( i ≤ n ) do
5:
i + +;
6:
end while
7:
k − −;
8: end while
What is the runtime of f unc2?
Algorithm 3 func3( int n )
1: i = n;
2: while ( i ≥ 1 ) do
3:
i = i/2;
4: end while
What is the runtime of f unc3?