run time complexity & its Big–O in data structure and algorith
run time complexity & its Big–O in data structure and algorith
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).
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).
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).
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)
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:
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
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.
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.