0% found this document useful (0 votes)
8 views

run time complexity & its Big–O in data structure and algorith

The document provides an analysis of the runtime complexity and Big-O notation for various code fragments and algorithms. It discusses complexities for loops, recursive functions, and operations on arrays, concluding with comparisons of algorithm efficiencies. Additionally, it suggests improvements for certain algorithms to optimize their performance.

Uploaded by

dawit kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

run time complexity & its Big–O in data structure and algorith

The document provides an analysis of the runtime complexity and Big-O notation for various code fragments and algorithms. It discusses complexities for loops, recursive functions, and operations on arrays, concluding with comparisons of algorithm efficiencies. Additionally, it suggests improvements for certain algorithms to optimize their performance.

Uploaded by

dawit kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

i.

For the Following code fragment determine the run time complexity and its Big–O
representation.
1. 0
2. Sum =0;
for ( i=0; i<n; i++)
Sum += i;
Ans:
Big-O: O(n)
Explanation: The loop i.e. for ( i=0; i<n; i++) iterates n times, the operation Sum += i is a
constant-time operation that occurs within this loop. Therefore, the runtime complexity of
the above code is O(n).

3. Sum =0;
for ( i=0; i<n; i++)
for ( j=0; j<n; j++)
Sum += j;
Ans:

Big-O: O(n2)

Explanation: The outer loop i.e. for ( i=0; i<n; i++) runs n times, and for each iteration of
the outer loop, the inner loop i.e. for ( j=0; j<n; j++) also runs n times. Thus, the runtime
complexity of the above code is n * n = O(n2).

4. Sum =0;
for ( i=0; i<n; i++)
for ( j=0; j<n*n; j++)
Sum += j;

Ans:

Big-O: O(n3)

The outer loop i.e. for ( i=0; i<n; i++) runs n times, and the inner loop (j=0; j<n*n; j++) runs n2
times. Therefore, the runtime complexity of the above code is n * n2 = n3.

5. Sum =0;
for ( i=0; i<n; i++)
for ( j=0; j<i; j++)
Sum += j;

Ans:

Big-O: O(n2)

1
The outer loop i.e. for ( i=0; i<n; i++) runs n times, and the inner loop (j=0; j<n; j++) runs n
times. Therefore, the runtime complexity of the above code is n * n2 = O(n3).

6. Sum =0;
for ( i=0; i<n; i++)
for ( j=0; j<i; j++)
for ( k=0; k<j; k++)
Sum += k;
}

Ans:
Big-O: O(n3)
The outer loop i.e. for ( i=0; i<n; i++) runs n times. The middle loop i.e. for ( j=0; j<i;
j++) runs n times for each i, and the innermost loop i.e. for ( k=0; k<j; k++) runs n times
for each j. This results in a nested loop of depth three, hence, the runtime complexity of
the above code is n*n*n = O(n3).

7. for (int count = 0, i = 0; i < N; i++) {


for (int j = i; j < N; j++) {
if(j % 2 == 0) break;
count++;
}}

Ans:
Big-O: O(N)

The outer loop runs N times. The inner loop runs until it finds an even number due to
break statement that exits the loop if j is even (j % 2 == 0) , thus, the runtime
complexity of the above code is, O(N).

8. for (long count = 0, i = N; i > 0; i--){


long j = i;
while (j >0){
count+=j;
j/= 2;}
}

Ans
Big-O: O(N log N)

2
The outer loop runs N times. The inner loop (while loop) runs logarithmically i.e.
O(log(N)) times. Hence, the runtime complexity of the above code is, N * O(log(N))
i.e. O(NlogN).

9. long factorial (int x) {


if (x > 0)
return x * factorial(x - 1);
else
return 1;
}
Ans:
Big-O: O(x)
The recursive function calls itself x times. The depth of the recursion is x because it
decrements x until it reaches 0, therefore, the execution time is linear and the runtime
complexity of the above factorial function is, O(x).

ii. Without writing an algorithm, tell the Big-Oh representation of the following problems.
Whenever there is an array assume the size of the array is big enough.
a. Searching an element from an unsorted array of elements. Do you think there will be
an improvement in the Big-Oh if the elements are ordered?
Ans: Big-O: O(n)
Improvement with ordered elements: Yes, if the array is ordered, binary search can
be used, which has a complexity of O(log n).
b. Deleting the ith element from unsorted array of elements.
Ans: Big-O: O(n)
c. Deleting an element from the end of the array.
Ans: Big-O: O(1)
d. Inserting an element at the ith position of the array.
Ans: Big-O: O(n)
e. Inserting an element at the end of the array.
Ans: Big-O: O(1)
f. Searching an element in two dimensional array of elements.
Ans: Big-O: O(n*m)
g. Finding the maximum element in a list of numbers
Ans: Big-O: O(n)
h. Finding the maximum element in a sorted list of numbers
Ans: Big-O: O(1)

i. Adding two Matrices.


3
Ans: Big-O: O(n*m)
j. Computing the∑𝑛𝑖=0 ∑𝑖𝑗=0 6
Ans: Big-O: O(n2)
𝑗
k. Computing the∑𝑛𝑖=0 ∑𝑖𝑗=0 ∑𝑘=0(2𝑘 + 1)
Ans: Big-O: O(n3)
iii. A student claims that his algorithm has a running time given as T(n)=3n -2n2+ 3, do you
agree with his claim? If not state your reason.

Ans: No, the leading term is -2n2, which implies the algorithm runs in O(n2), not linear.
The negative coefficient does not affect the Big-O notation since only the highest order
term is considered.
iv. If f(n) = 3n2 + 4n + 1, show that f(n) is O(n2) .

Ans:

→f(n)≤C⋅n2 for all n≥n0 where C is constant value


→3n2+4n+1≤C⋅n2
→3n2+4n+1≤Cn2
→3n2-Cn2+4n+1≤0
→ (3-C) n2+4n+1≤0
→Let C=0
→ (3-0) n2+4n+1≤0
→ 3n2+4n+1≤0

Since the leading term is 3n2, the runtime complexity of the above code is O(n2).

v. Determine the running time and Big-O of the following algorithms and suggest an
improvement, or a better algorithm altogether, and indicate its efficiency class

long ASum(int n){ double Pow( ldouble x, int n ){


long sum =0; long p=1;
for ( long i=1; i<=n; i++) if(x==0)
sum += i; return 0;
return sum; else{
} for(int i=1; i<=n; i++)
p=p*x;
}
return p;}

Ans:
Algorithm 1: Sum of First n Natural Numbers

4
long ASum(int n) {
long sum = 0;
for (long i = 1; i <= n; i++)
sum += i;
return sum;
}
Running Time:
The loop runs n times, performing a constant-time operation (sum += i) in each iteration.
Big-O: O(n)
Suggestion for Improvement:
This algorithm can be improved using the formula for the sum of the first n natural
numbers:
𝑛(𝑛+1)
Sum=
2

This reduces the time complexity to O(1) since it only requires a few arithmetic
operations.

Algorithm 2: Power Calculation


double Pow(ldouble x, int n) {
long p = 1;
if (x == 0)
return 0;
else {
for (int i = 1; i <= n; i++)
p = p * x;
}
return p;
}
Running Time:
The loop runs n times, multiplying p by x in each iteration; therefore, the Big-O is O(n).

Suggestion for Improvement:


This algorithm can be improved using Exponentiation by Squaring, which reduces the
time complexity significantly:
o If n is even, Pow(x,n)=Pow(x,n/2)×Pow(x,n/2)
o If n is odd, Pow(x,n)=x×Pow(x,n−1)
This approach’s runtime complexity or Big-O is O(log n).

vi. Algorithm ‘A’ does a certain task in a ‘time’ n3+ 200n + 1000 where n is the number of
elements processed. Algorithm ‘B’ does the same task in a ‘time’ of 30n2 + 1000.
5
a) What are the Big-O requirements of each of the algorithms?
Ans:
Big-O:
• Algorithm A: O(n3)
• Algorithm B: O(n2)
b) Which algorithm is more efficient by Big-O standards?
Ans:
Algorithm B is more efficient by its Big-O since O(n2) grows slower than O(n3) as
n increases. That means O(n2) will perform better (i.e., take less time) than O(n3)
for large inputs.
c) Under what conditions, if any, would the less efficient algorithm execute more
quickly than the more efficient algorithm?
Ans:
For small values of n, the constant factors in Algorithm A might make it faster than
Algorithm B, despite its higher Big-O complexity. That is, the impact of n3 is not
significant at small n.

vii. The input is an N by N matrix of numbers that is already in memory. Each individual row
is increasing from left to right. Each individual column is increasing from top to bottom.
Give an O(N) worst-case algorithm that decides if a number X is in the matrix.
Ans:
Big-O: O(N)
We can start from the bottom left (or top right) of the matrix and use a strategy where we
move right if the number is smaller than X, or up if it is larger. This allows us to
potentially eliminate rows or columns, resulting in a linear search in terms of N.

You might also like