Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Assignment 1 Fall202223

Uploaded by

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

Assignment 1 Fall202223

Uploaded by

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

Total = 50 marks (Note upload one word/pdf file in the blackboard)

1) [20 marks] Write at least one real world example for each of the following complexity types:

O(1), O(lg n), O(n), O(nlgn), O(n2), O(2n).


For each of the above, write the actual aglorithm in pseudo-code and explain how the time complexity is
evaluated.

2) [30 marks] Do the exact analysis as well as Big O analysis for the time complexity of the following
algorithms. Assume that all operations (arithmetic, logical, read/write take 10 nanoSecond each).

For example, your answer should be as detailed as shown in the following example:

Lets say the algorithm is as follows:

for (int i = 1; i <= n; i++)


{
int a = 4;
int b = 5;
int c = a + b;

Exact analysis:

for (int i = 1 to n) //loop is running ‘n’ times


{
int a = 4; //memory write operation that will take 10 ns.
int b = 5; //memory write operation that will take 10 ns.
int c = a + b; //it involves 2 memory reads for a and b, one mathematical
//operation + and one memory write for c, so total time is 40 ns.

So total time = n (10+10+40) = (60n) nanoseconds

Asymptotic analysis (Big –O):

for (int i = 1 to n) //loop is running ‘n’ times


{
int a = 4; //constant time.
int b = 5; //constant time.
int c = a + b; //constant time.

So total time = n (c+c+c) = (n*c) = O(n)


1) A function (or set of statements) that doesn’t contain loop, recursion and call to any other non-
constant time function, such as follows:
int x = 5;
int y = 4;
int z = x + y;
int t = x + z;

2) A loop or recursion that runs a constant number of times, such as follows:


for (int i = 1 to c) // Here c is a constant
{
int a = 5;
a++;
a--;
}

3) A loop where the loop variables is incremented / decremented by a constant amount, as


follows:
// Here c is a positive integer constant
for (int i = 1; i <= n; i += c) {
int a = 5;
a+=i ;
}

4) Time complexity of nested loops as follows:


// Here c is a positive integer constant
for (int i = 1; i <=n; i += c) {
for (int j = 1; j <=n; j += c) {
int a = i + j;
}
}

5) Time Complexity of a loop if the loop variables is divided / multiplied by a constant amount as
follows:
for (int i = 1; i <=n; i *= c) {
int a = i + i ;
}

You might also like