0% found this document useful (0 votes)
66 views4 pages

College of Computing and Information Technology

This document contains a sheet for a computing algorithms course taught by Dr. Nahla Belal. The sheet includes exercises on analyzing time complexities of algorithms, describing algorithms to solve problems like finding pairs that sum to a target number, and performing matrix transposition. It provides multiple choice and short answer questions to help students practice analyzing time complexities of algorithms.

Uploaded by

Samir Husein
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)
66 views4 pages

College of Computing and Information Technology

This document contains a sheet for a computing algorithms course taught by Dr. Nahla Belal. The sheet includes exercises on analyzing time complexities of algorithms, describing algorithms to solve problems like finding pairs that sum to a target number, and performing matrix transposition. It provides multiple choice and short answer questions to help students practice analyzing time complexities of algorithms.

Uploaded by

Samir Husein
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/ 4

College of Computing and Information Technology

Lecturer: Dr. Nahla Belal


Course: Computing Algorithms (CS312)
TA:

Sheet 1
1. Fill in the following table by selecting one of the following: f(n) = O(g(n)), f(n) = (g(n)),
or f(n) = (g(n)). (log n = log2 n).

f(n) g(n)
n3 n6
n3 n3log n
n3 2n log n
n3 n2n
n3 22
n

n6 n3log n
n6 2n log n
n6 n2n
n6 22
n

n3log n 2n log n
n3log n n2n
n3log n 22
n

2n log n n2n
2n log n 22
n

n2n 22
n

2. Prove the following:


n2
a.  2 n  O( 2 n )
10
1
b. 21   O(1)
n
c. 10 log 3n  O(n)
d. log n3 = O(log n)
e. (O(n)+102) = O(n)
3. What is the time complexity of the following algorithms:
a. for(int i =0 ; i < =n ; i++)
for(int j =1; j<= i * i; j++)
if (j % i == 0)
for(int k = 0; k<j; k++)
sum++;

Ans: O(n4)

b. int x=0;
for(int i=4*n; i>=1; i--)
x=x+2*i;

Ans: O(n)

c. int z=0;
int x=0;
for (int i=1; i<=n; i=i*3){
z = z+5;
z++;
x = 2*x;
}

Ans: O(log3 n)

d. int y=0;
for(int j=1; j*j<=n; j++)
y++;

Ans: O(n1/2)

e. int b=0;
for(int i=n; i>0; i--)
for(int j=0; j<i; j++)
b=b+5;

Ans: O(n2)

f. int y=1;
int j=0;
for(j=1; j<=2n; j=j+2)
y=y+i;
int s=0;
for(i=1; i<=j; i++)
s++;

Ans: O(n)
g. int b=0;
for(int i=0; i<n; i++)
for(int j=0; j<i*n; j++)
b=b+5;

Ans: O(n3)

h. int x=0;
for(int i=1; i<=n; i=i*3){
if(i%2 != 0)
for(int j=0; j<i; j++)
x++;
}

Ans: O(n)

i. int t=0;
for(int i=1; i<=n; i++)
for(int j=0; j*j<4*n; j++)
for(int k=1; k*k<=9*n; k++)
t++;
Ans: n*2(n1/2)*3(n1/2) = O(n2)

j. int a = 0;
int k = n*n;
while(k > 1){
for (int j=0; j<n*n; j++)
{ a++; }
k = k/2;
}

Ans: log(n2)*n2 = O(n2 log n)

k. int i=0, j=0, y=0, s=0;


for(j=0; j<n+1; j++)
y=y+j;
for(i=1; i<=y; i++)
s++;

Ans: O(n2)

4. What is the time complexity of the following algorithm:


1 m=a
2 i=1
3 while ( i <= n ) do
4 m=m*b
5 for i = 1 to m do
6 S
7 end for
8 end while
9 end

S is O(1).

5. Describe an O(n log n)-time algorithm that, given a set S of n integers and another integer k,
determines whether or not there exists two elements in S whose sum is exactly k.

6. Describe an O(log n)-time algorithm that finds x^n.

7. Describe an algorithm that performs matrix transposition for an n x n matrix. Transposition


is defined as interchange of the elements aij and aji, for i = 1, 2, …, n and j = 1, 2, …, n-1.
Analyze the time and space complexity of your algorithm.

You might also like