You are on page 1of 3

BCS

BCS-DS-50: Design and Analysis of Algorithms

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.

def LinearSearch(arr, n,, item):


item

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

2. Binary search takes a sorted


rted array as input, and uses Divide and Conquer
Conquer to find the given element.
Write the algorithm for Binary
inary Search and find the complexity.

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:

for(i=0, i<n; i++)

for (j=i+1, j<n ; j++)

if(arr[j] < arr[i])

{
swap(arr[i], arr[j])

return arr

State the number of times each statement is executed and find the complexity.

5. Consider the following snippet, where arr is an array having n elements:

for(i=0, i<n; i++)

for (j=0, j<n-i ; j++)

if(arr[j+1] < arr[j])

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(n) = fib(n-1) + fib(n-2)

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.

10. Can you accomplish the above task in O(n)?

Theory

1. Define the following:

i) Upper bound: O

ii) Lower Bound: Omega and

iii) Tight bound: Theta

2. Is Upper Bound same as worst case? Discuss.

3.

a) Define an algorithm. Write the characteristics of an algorithm.

b) Differentiate between a program and an algorithm.

c) Which data structure can be used to convert a recursive algorithm to a non-recursive algorithm?

You might also like