Professional Documents
Culture Documents
Tutorial 1
Topic: Complexity
1.. The following code implements Linear Search. If the size of the iinput
put array is n, find the number of
times each step will be executed and the complexity of the algorithm.
flag = 0
for I in range(n):
if(arr[i]
rr[i] == item):
print(‘Found at ‘ , i)
flag = 1
if(flag ==0):
print(‘Not
ot Found’)
Found
3.. If in the above algorithm, the given array is divided in to three parts instead of two, what will be the
complexity? (Ternary Search)
4.. Consider the following snippet, where arr is an array having n elements:
{
swap(arr[i], arr[j])
return arr
State the number of times each statement is executed and find the complexity.
swap(arr[j+1], arr[j])
return arr
State the number of times each statement is executed and find the complexity.
6. In a given array, the first k elements are already sorted, you need to insert the (k+1) the element at its
appropriate position. Write an algorithm and find its complexity.
7. The nth term of the Fibonacci series can be found by using the following formula:
fib(1) = 1
fib(2) = 1
Find the complexity of finding the nth term of the Fibonacci series, using recursion and find its
complexity.
8. Suggest some other method to find the nth term of the Fibonacci series, in O(n).
9. Find the frequency of each term in a given array.
Theory
i) Upper bound: O
3.
c) Which data structure can be used to convert a recursive algorithm to a non-recursive algorithm?