Blatt 02
Blatt 02
Blatt 02
Exercise Sheet 2
for the lecture on
• For each line of code, write down the number of running steps (dependent on
the input size) in the worst case.
• Sum up the total number T (n) of steps the algorithm needs in the worst case
for an input of size n.
• Provide a function f (n) as a representative upper bound in O-notation.
• Proof formally that T (n) ∈ O(f (n)) holds.
Problem 2 for discussion: Refactoring
Take a look at the following Python implementation of selection_sort.
1 def selection_sort ( A ):
2 for j in range ( len ( A )):
3 m=j
4 for i in range ( j +1 , len ( A )):
5 if A [ i ] < A [ m ]:
6 m = i
7 key = A [ j ]; A [ j ] = A [ m ]
8 A [ m ] = key
9 return A
How can we optimise the readability and reusability of this code with respect to the
following criteria?
• consistency (e.g. spacing)
• expressivity (e.g. variable names)
• function extendability (unique purpose, brevity, testability)
• avoiding redundancy
Discuss: How do these changes affect the running time of the algorithm?