Professional Documents
Culture Documents
Exercise 1
Exercise 2
Exercise 3
For each of the following functions, indicate the class Θ(g(n)) the function
belongs to. (use the simplest g(n) possible in your answers). prove your
assertions.
• (n2 + 1)1 0
√
• 10n2 + 7n + 3
• 2n lg (n + 2)2 + (n + 2)2 lg n2
1
Algorithm 1 ComparisonCountingSort(A[0 . . . n-1])
1: {Sorts an array by comparison counting}
2: Input: Array A[0 . . . n − 1] of values.
3: Output: Array S[0 . . . n − 1] of values.
4: for i=0 to n-1 do
5: Count[i] = 0
6: end for
7: for i=0 to n-2 do
8: for j=i+1 to n-1 do
9: if A[i]¡A[j] then
10: Count[j] = Count[j] + 1
11: else
12: Count[i] = Count[i] + 1
13: end if
14: end for
15: end for
16: for i=0 to n-1 do
17: S[Count[i]] = A[i]
18: end for
19: return S
• 2n+1 + 3n−1
• ⌊log2 n⌋
Exercise 5
List the following functions according to their order of growth from the
lowest to the highest:
• (n − 2)!
• 5 lg (n + 100)1 0
• 22
n
• 0.001n4 + 3n3 + 1
• ln2 n
2
√
• 3
n
• 3n
Exercise 6
• 1 + 3 + 5 + 7 + . . . + 999
• 2 + 4 + 8 + 16 + . . . + 1024
∑n+1
• i=3 1
∑n+1
• i=3 i
∑n−1
• i=0 i(i + 1)
∑n j+1
• j=1 3
∑n ∑n
• i=1 j=1 ij
∑n
• 1
i=1 i(i+1)
Exercise 7
Find the order of growth of the following sums. Use the Θ(g(n)) with the
simplest function g(n) possible.
∑n−1
• i=0 (i2 + 1)2
∑n−1
• i=2 lg i2
∑n
• + 1)2i−1
i=1 (i
∑n−1 ∑n−1
• i=0 j=0 (i + j)
Exercise 8
3
Algorithm 2 Mystery(n)
1: Input: A non-negative integer n.
2: S = 0
3: for i=1 to n do
4: S =S+i×i
5: end for
6: return S
Exercise 9
4
Exercise 10
Exercise 11
5
Algorithm 5 S(n)
1: Input: A positive integer n.
2: Output: The sum of the first n cubes
3: if n = 1 then
4: return 1
5: return false
6: else
7: S(n − 1) + n × n × n
8: end if