Design and Analysis of Algorithms
Fall 2025
Practice Problems
Problem 1
Find the best case and worst case time complexity for the following code segments and show working
along with an example for best and worst case scenarios. Clearly give the time complexity function as
well as the asymptotic growth rate (Give tight bounds).
GCD Code fragment
int GCD(int n,int m){
int min;
if(n<m)
min = n;
else
min = m;
for(int i=min; i>1; i--)
if(n%i ==0 && m%i==0)
return i;
return 1;
}
Code fragment that returns the binary representation of num in arr
void binary(int num, int arr[], int &bits)
{
int temp = num;
bits = 0;
while(temp>0){
temp = temp/2;
bits++;
}
int i = bits-1;
while(num > 0){
arr[i] = num%2;
i--;
num = num/2;
}
}
Code fragment for bubble sort
void bubbleSort(int arr[],int n)
{
bool done = false;
for(int i = 1; (i < n) && !done; i++)
{
done = true;
for (int j=0; j <n-i; j++)
{
if (arr[j+1] < arr[j])
{
swap(arr[j+1],arr[j])
done = false;
}
}
}
}
//a swap is made and so sorting continues
Problem 2
Rank the following functions by order of growth; that is, find an arrangement g1, g2, . . . g20 of
the functions satisfying g1=O( g2),g2 = O (g3), . . . , g19 = O (g20). Partition your list into
equivalence classes such that functions f (n) and g(n) are in the same class if and only if f (n) =
θ(g(n)).
𝑙𝑔𝑛
(3/2)𝑛
𝑛2
𝑛!
(𝑙𝑔𝑛)!
√2
𝑛3
𝑛. 2𝑛
𝑛𝑛
(𝑙𝑔𝑛)2
2𝑙𝑔𝑛
22
lg(𝑛!)
𝑒𝑛
𝑛𝑙𝑔𝑛
𝑛
22
4𝑙𝑔𝑛
1
𝑙𝑛𝑙𝑛(𝑛)
(𝑛 + 1)!
𝑛
Problem 3
Indicate, for each pair of expressions .(A,B) in the table below, whether A is O,Ω or θ of B.
Assume that k >= 1, ϵ > 0, and c > 1 are constants. Your answer should be in the form of the
table with “yes” or “no” written in each box
A
B
O
Θ
Ω
logk(n)
n1-ϵ
nk
5cn
n
2
2n+1
2n/2
2n
nlgc
clgn
1/2
n
lg(n2)
lg(n!)
lg(nn)