Professional Documents
Culture Documents
Lab00
Lab00
Lab00
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: n
2. Select the basic operation: addtion operation in line 4 (X = X + ...)
3. Identify the worst cases: none
4. Count the number of executions of the basic operation: T ( n )=n−1
5. Determine the efficiency class of the algorithm: T ( n ) ∈ θ(n)
2. Design and analyze a non-recursive algorithm to find the sum of the
following series.
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: n
2. Select the basic operation: addtion operation in line 4 (Y = Y + ...)
3. Identify the worst cases: none
4. Count the number of executions of the basic operation: T ( n )=n
5. Determine the efficiency class of the algorithm: T ( n ) ∈ θ(n)
3. Design and analyze a non-recursive algorithm to find the sum of the
following series.
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: n
2. Select the basic operation: addtion operation in line 4 (Z = Z + ...)
3. Identify the worst cases: none
4. Count the number of executions of the basic operation: T ( n )=n
5. Determine the efficiency class of the algorithm: T ( n ) ∈ θ(n)
4. Design and analyze a non-recursive algorithm to find the factorial of a
positive integer n, denoted by n!
a) Use Python to present the algorithm.
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: n
2. Select the basic operation: multiplication operation in line 4 (f = f * i)
3. Identify the worst cases: none
4. Count the number of executions of the basic operation: T ( n )=n
5. Determine the efficiency class of the algorithm: T ( n ) ∈ θ(n)
6. Design and analyze a non-recursive algorithm to check whether all elements
in an array are distinct
a) Use Python to present the algorithm.
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: n (array length)
2. Select the basic operation: comparision operation in line 5 (if A[i] ==
A[j])
3. Identify the worst cases: all elements in array are distinct
4. Count the number of executions of the basic operation:
i=0: inner loop runs n-1 times
i=1: inner loop runs n-2 times
i=2: inner loop runs n-3 times
...
i=n-3: inner loop runs 2 times
i=n-2: inner loop runs 1 times
(n−1) × n n 2−n
T ( n )= =
2 2
5. Determine the efficiency class of the algorithm: T ( n ) ∈ θ(n2 )
8. Design and analyze a non-recursive algorithm to find the maximum element
in an array, supposing that all elements in the array are unique.
a) Use Python to present the algorithm.
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: row, col (number of rows and
columns)
2. Select the basic operation comparison: substraction in line 6
3. Identify the worst cases: none
4. Count the number of executions of the basic operation:
T ( row , col )=row . col
5. Determine the efficiency class of the algorithm:T ( row , col ) ∈θ( row . col)
15. Design and analyze an algorithm for adding (+) two matrices
a) Use Python to present the algorithm.
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: row, col (number of rows and
columns)
2. Select the basic operation comparison: addition operation in line 6
3. Identify the worst cases: none
4. Count the number of executions of the basic operation:
T ( row , col )=row . col
5. Determine the efficiency class of the algorithm:
T ( row , col ) ∈θ( row . col)