You are on page 1of 2

Technische Hochschule Ingolstadt

Prof. Dr.-Ing. Richard Membarth

27. March 2024

Algorithms and Data Structures


Assignment 1
Submission deadline: 31. March 2024

Order of Growth
Question 1 Math Functions
Determine the complexity class of the following functions using the O-notation:

f 1(n) = 2(n − 1)(n + 1)/n + log n


f 2(n) = log n2
n
X
f 3(n) = i
i=1

Is O(2n+1 ) = O(2n )?
Is O(22n ) = O(2n )?

Question 2 Python Functions


The functions body(i) and body(i, j) require constant time, independent of i and j. Give
the complexity of the following functions f1, f2, f3, and f4 using the O-notation depending on
n:
1 k = ...
2
3 def f1 ( n ):
4 for i in range (0 , n ):
5 body ( i )
6
7 def f2 ( n ):
8 for i in range (0 , k ):
9 body (i , n )
10
11 def f3 ( n ):
12 for i in range (0 , n ):
13 f1 ( i )
14

1
15 def f4 ( n ):
16 step = n
17 while n > 0:
18 for start in range (0 , step ):
19 for i in range ( start , n , step ):
20 body ( i )
21 n = n //2
Hint: the // operator is the floor division operator in Python. This prevents conversion of the
division result to float in Python 3.
Hint: range(start, stop, step): step defines the increment between numbers in the sequence (1
if omitted), the corresponding C code for f4 is:
1 void f4 ( int n ) {
2 for ( step = n ; n > 0; n /= 2)
3 for ( int start = 0; start < step ; start ++)
4 for ( int i = start ; i < n ; i += step )
5 body ( i );
6 }

Question 3 Algorithms
For the sorting algorithms “AKS” and “BITONIC” are the number of operations given as:

#opAKS = 1000n log n


#opBITONIC = 0.5n(log n)2

Which algorithm has the better runtime according to the O-notation?


Which algorithm would you use in practice? Explain your choice!

You might also like