Lab 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Enrollmentno:190470107023 LAB-5

Implement and Analyze Fibonacci Program using Iterative and


Lab 5:
Recursive Method.
Enrollmentno:190470107023 LAB-5

• Fibonacci Series (Iterative)


Program:

a=int(input("Enter the first element: "))


b=int(input("Enter the second element: "))

c=int(input("Enter the number of terms which you needed:"))


print(a,b,end=" ")
while(c-2):
t=a+b
a, b = b, t
print(b,end=" ")
c=c-1
Enrollmentno:190470107023 LAB-5

Analysis:
Enrollmentno:190470107023 LAB-5

• Fibonacci Series (Recursive)


Program:

def fibonacci_series(n):
if n <= 1:
return n
else:
return(fibonacci_series(n-1) + fibonacci_series(n-2))

terms = int(input("Enter the element which you want display in the fibonacci series:"))

if terms <= 0:
print("Enter the positive number in input")
else:
print("fibonacci series :")
for i in range(terms):
print(fibonacci_series(i))
Enrollmentno:190470107023 LAB-5

Analysis:
Enrollmentno:190470107023 LAB-5

Question 1: Consider the problem of computing min-max in an unsorted array where


min and max are minimum and maximum elements of array. Algorithm A1 can
compute min-max in a1 comparisons without divide and conquer. Algorithm A2 can
compute min-max in a2 comparisons by scanning the array linearly. What could be
the relation between a1 and a2 considering the worst-case scenarios?

• When Divide and Conquer is used to find the minimum-maximum element in an


array, Recurrence relation for the number of comparisons is
T(n) = 2T(n/2) + 2 where 2 is for comparing the minimums as well the maximums
of the left and right subarrays
So, the answer is a1 > a2.

Question 2: Let P be an array containing n integers. Let t be the lowest upper bound on
the number of comparisons of the array elements, required to find the minimum and
maximum values in an arbitrary array of n elements. Which one of the following
choices is correct?
a. t > 2n − 2
b. t > 3⌈n / 2⌉ and t ≤ 2n − 2
c. t > n and t ≤ 3⌈n / 2⌉
d. t > ⌈lg(n)⌉ and t ≤ n

Answer: __ B____

Question 3: Consider the Quicksort algorithm. Suppose there is a procedure for finding
a pivot element which splits the list into two sub-lists each of which contains at least
one-fifth of the elements. Let T(n) be the number of comparisons required to sort n
elements. Then

a. T(n) ≤ 2T(n/5) + n
b. T(n) ≤ T(n/5) + T(4n/5) + n
c. T(n) ≤ 2T(4n/5) + n
d. T(n) ≤ 2T(n/2) + n

Answer: __B___

You might also like