Lab00

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

1.

Design and analyze a non-recursive algorithm to find the sum of the


following series.

a) Use Python to present the algorithm.

b) Implement the algorithm in a compiled programming language (Java)

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.

a) Use Python to present the algorithm.

b) Implement the algorithm in a compiled programming language (Java)

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.

a) Use Python to present the algorithm.

b) Implement the algorithm in a compiled programming language (Java)

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.

b) Implement the algorithm in a compiled programming language (Java)

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.

b) Implement the algorithm in a compiled programming language (Java)

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.

b) Implement the algorithm in a compiled programming language (Java)


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 comparison: if A[i] > max
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)
11. Design and analyze a non-recursive algorithm to find an element in an
array sorted in ascending order.
a) Use Python to present the algorithm.

b) Implement the algorithm in a compiled programming language (Java)


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 comparison: comparision operation in line 5
3. Identify the worst cases: x is not in array A
4. Count the number of executions of the basic operation: T ( n )=log n
5. Determine the efficiency class of the algorithm: T ( n ) ∈ θ(log n)
12. Design and analyze an algorithm for multiplying (x) two matrices.
a) Use Python to present the algorithm.
b) Implement the algorithm in a compiled programming language (Java)
c) Determine the time efficiency of the algorithm [in the worst case] (as a function
of input size).
1. Find variables indicating input size: m, n, p
2. Select the basic operation comparison: multiplication operation in line 9
3. Identify the worst cases: none
4. Count the number of executions of the basic operation:
T ( m , n , p )=m. n . p
5. Determine the efficiency class of the algorithm:
T ( m , n , p ) ∈θ (m. n . p)
13. Design and analyze an algorithm for multiplying (x) a matrix with a
number.
a) Use Python to present the algorithm.

b) Implement the algorithm in a compiled programming language (Java)


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: multiplication operation in line 9
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)
14. Design and analyze an algorithm for subtracting (-)  two matrices.
a) Use Python to present the algorithm.
b) Implement the algorithm in a compiled programming language (Java)

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.

b) Implement the algorithm in a compiled programming language (Java)

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)

You might also like