0% found this document useful (0 votes)
6 views2 pages

Worksheet 1

The document is a worksheet for a Data Structures and Algorithms course, focusing on Big-O notation and algorithm efficiency. It includes exercises on determining dominant terms, analyzing algorithm complexity, and comparing different algorithms. The worksheet also asks for examples of algorithms with specific time complexities and discusses recursive functions.

Uploaded by

Alemseged Mammo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Worksheet 1

The document is a worksheet for a Data Structures and Algorithms course, focusing on Big-O notation and algorithm efficiency. It includes exercises on determining dominant terms, analyzing algorithm complexity, and comparing different algorithms. The worksheet also asks for examples of algorithms with specific time complexities and discusses recursive functions.

Uploaded by

Alemseged Mammo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CPU

Department of Computer Science


Data Structure and Algorithms
Worksheet I
1. Suppose each of the following expressions represent the number of key operations in an
algorithm as a function of n. For each expression, determine the dominant term and then
classify the algorithm in Big-O terms.
a) n2/2 + 3000 n
b) 60n2 +2n4 + 3 2n+3n
c) n20 + 2n
d) nlogn
e) log n + ((5/2)*(0.4))n
f) 0.0001 n2 + 10000 n – (6n5 + 3)/n4
g) 48 n4 + 16 n2 + log 8 n + 2n
2. Perform a Big-O analysis for the following statements.
a) b)
int i, j, n; int i, n, j = 1;
for (i = 0; i < n; i++){ for (i = 0; i < n; i++)
j = n; j = j * 2;
while(j > 1){ do{
…; …;
…; …; j-
j /= 2; -;
} } while(j >= 0);

3. Give at least one example of an algorithm that is:


a) O(1);
b) O(n log 2 n); c) O(log 2 n); d) (n2) e) 2n f)3n h) n! I) nn
4. Algorithm ‘A’ does a certain task in a ‘time’ n3 + 300n where n is the number of input
processed. Algorithm ‘B’ does the same task in a ‘time’ of 30n2 + 1000.
a) What is the complexity of each algorithms interims of Big-O?
b) Which algorithm is more efficient?
c) Under what conditions, if any, would the less efficient algorithm execute more
quickly than the more efficient algorithm?
5. let f(n)=n2, then f(n)=O(n4), f(n)=O(n3) are f(n)=O(n2) technically correct but . but the last
one is technically the most appropriate. Why?
6. What is the big-O of an algorithm whose complexity can be defined recursively according to
the following function:
f(n)=2f(n/2)+ n ,f(1)=1
7. Consider to algorithms whose time complexity can be expressed interims of the following
two functions. f(n)=210n
g(n)=n2
a) Which of the functions grows at a faster rate?
b) What is the value of the turning point?
8. Among the two algorithms for each of the two exercises below, select one which is more
efficient interims of CPU time. Justify your answer using big-O.

1
a) Algorithms that generate the first n elements of a Fibonacci series.

Algorithm-I Algorithm-II
void fibonacci_1(long n) void fibonacci_2(long n)
{ {
long i; long f,f1=0,f2=1,i;
if(n<0) for(i=0;i<=n;i++)
cout<<"Invalid Input"; {
else f=f1+f2;
for(i=1;i<=n;i++) cout<<f;
cout<<fibo(i); f2=f1;
cout<<endl; f1=f;
} }
long fibo(long n) }
{

if((n==1)||(n==2))
return 1;
else
return
fibo(n-1) +fibo(n-2);
}

b) Algorithms, which compute the summation the factorial of the first n natural
numbers.
int fact(int n) void main()
{ {
if(n==1) int n,i;
return 1; long sumf=0,facti=1;
else cout<<"Enater a natural
return n* fact(n-1); number";
} cin>>n;
void main() for(i=1;i<=n;i++)
{ {
int n,i; facti=facti*i;
long sumf=0; sumf=sumf+ facti;
cout<<"Enater a natural number"; }
cin>>n; cout<<sumf;
for(i=1;i<=n;i++)
sumf=sumf+ fact(i); }
cout<<sumf;

***** Do all questions (Exercises 2.11) on the text book staring


from page 71

You might also like