CS2002 - Data Structures and Algorithms: Assignment Number 1
CS2002 - Data Structures and Algorithms: Assignment Number 1
Instructions:
• Partially or fully copied assignments will be marked as zero.
• Only handwritten solution on A4 page will be accepted.
• Use the title page provided with this assignment.
• Late submissions are not allowed.
• Solve all the questions in the given order.
• Justify your answers with proper reasons/calculations. No marks will be awarded in
case of missing calculations/reasons.
Taxonomy
CLO No. CLO Statement Domain
Level
Compute the efficiency of algorithms and data
1 Cognitive 3
structures in terms of time and space complexity.
void fun(int n)
void fun(int a, int b) {
{ int i = 1;
// a and b are +ive integers while (i < n)
while (a != b) {
{ int j = n;
if (a > b) while (j > 0)
a = a - b; {
else j = j / 2;
b = b - a; }
} i = i * 2;
} }
}
#include<iostream>
#include<math.h>
using namespace std;
void JudgePrime(int n)
{
if(n < 2)
cout<<"Not Prime"<<endl;
for(int i = 2; i<=sqrt(n); i++)
{
if(n % i == 0)
{
cout<<"Not Prime"<<endl;
exit(0);
}
}
cout<<"Prime"<<endl;
}
main()
{
int n;
cout<<"Enter a Number : ";
cin>>n;
JudgePrime(n);
}
The maximum number of iterations in the function are √𝑛 , hence the complexity is 𝑂(√𝑛).
If n>=k then the time complexity will be It is a diverging harmonic series, and the sum of
considered in O((nk+1)/(k+1)) this series is given as (logn).
And if n<k, then the time complexity will be Hence the time complexity becomes O(nlogn).
considered as in the O(nk)
void function(int n)
{
int i = 1, s = 1;
while (s <= n)
{
i++;
s += i;
cout<<“++”;
}
}
We can define the terms “s” according to relation si = si-1 + i. The value of ‘i’ increases by
one for each iteration. The value contained in “s” at the ith iteration is the sum of the first ‘i’
positive integers. If k is total number of iterations taken by the program, then while loop
terminates if:
1 + 2 + 3 + 4 + ⋯+ 𝑘 > 𝑛
The sum of first k positive integers can be given as:
𝑘(𝑘 + 1) 𝑘 2 + 𝑘
1 + 2 + 3 + 4 + ⋯+ 𝑘 = =
2 2
Hence the termination condition will become
𝑘2 + 𝑘
>𝑛
2
We can notice that k is a second-degree polynomial and while writing the complexity in big-
oh notation we will ignore the leading coefficients and polynomials. Hence the termination
condition will be
𝑘2 > 𝑛
Solving for k we get
𝑘 > √𝑛
Hence the complexity is 𝑂(√𝑛).
(1) sum = 0;
for(int i = 0; i < n; i+=2)
for(int j = 0; j < n*n; j++)
sum++;
(2) sum = 0;
for(int i = 1; i <= n; i*=2)
for(int j = 0; j < i; j++)
sum++;
(3) sum = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < i*i; j++)
for(int k = 0; k < j; k++)
sum++;
(4) sum = 0;
for(int i = 1; i < n; i++)
for(int j = 1; j < i*i; j++)
if(j % i == 0)
for(int k = 0; k < j; k++)
sum++;
(5) int i, j, k = 0;
for (i = n / 2; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n / 2;
(6) int a = 0, i = n;
while (i > 0)
{
a += i;
i /= 2;
}
1) Here we have two nested for loops, the outer loop has a maximum of n iterations, and the
inner loop has a possible n2 iterations, hence the complexity is O(n3).
2) Here we have two nested for loops with n iterations in inner and log2n iterations in outer
loop, hence complexity is O(nlog2n).
3) Here we have three level nesting. The inner loop has maximum j iterations, the loop with
variable j has a maximum of n2 iterations and the outer loop has a maximum n iteration,
hence the complexity is O(n5).
4) Here j can be as large as n2, so the if statement is executed at most O(n3) times, but the
condition is true only O(n2) times, only i times for each j, hence the complexity is O(n4).
5) The outer loop will have n/2 iterations, due to single increment after each iteration. The inner
loop will have log(n) iterations because after each iteration the value of j is multiplied by 2.
Hence, the overall complexity will be O(n log(n)).
6) Here the termination condition of loop is decided by the variable i. the initial value of i is n
and after each iteration the value of i is divided by 2. Hence the complexity is O(log(n)).
1) There are n iterations in outer loop but there is a break statement in inner loop due to
which the inner loop will run only once. Hence the complexity is O(n).
2) The outer loop will have a maximum of n/2 iterations due to single increment in each
iteration. The loop with variable j will have log(n) iterations because after every
iteration the value of j is multiplied by 2. Similarly, the loop with variable k will have
log(n) iterations. Hence the complexity is O[(n/2) (log(n)) (log(n))] = O(nlog2(n)).
3) The outer loop will be having n/2 iterations due to single increment after each
iteration. The loop with variable j will be having n/2 iterations due to single increment
after each iteration. The loop with variable k will have log(n) iterations because the
value of k is multiplied by 2 after every iteration. Hence the complexity is
O[(n/2) (n/2) (log(n))] = O(n2log(n)).
Best Case
The best is when the key we want to find is located at index [0][0]. According to code, there
is an unconditional break statement in inner loop due to which the inner loop will run only
once, and the outer loop will be having a maximum of r iterations. Hence the best-case time
complexity is O(r).
Worst Case
The worst case will occur when the key we want to search is not present in the array at all
and we must search the entire array. But due to unconditional break statement in inner loop
there are a possible r iteration of the outer loop. Hence the worst-case time complexity is
O(r).
Accuracy of Code
The code is not accurate at all. It will search the key only in the first element of every row.
All other elements will go unchecked due to unconditional break statement.