Question

In: Computer Science

For each of the following six program fragments give an analysis of the running time (Big-Oh...

For each of the following six program fragments give an analysis of the running time (Big-Oh will do). (25,very 5 mark)

(a)sum = 0;
for( i=0; i<n; i++ )
for( j=0; j<n; j++ )
sum++;
(b)
sum = 0;
for( i=0; i<n; i++ )
for( j=0; j<n*n; j++ )
sum++;

(c)
sum = 0;
for( i=0; i<n; i++ )
for( j=0; j<i; j++ )
sum++;
(d)
sum =0;
for( i=1; i<n; i++ )
for( j=1; j<i*i; j++ )
if( j%1 == 0 )
for( k=0; k<j; k++ )
sum++;

Solutions

Expert Solution

(a)

sum = 0;
for( i=0; i<n; i++ )
for( j=0; j<n; j++ )
sum++;

Let's first understand the above code line by line.

1. sum = 0. This is simply an initialization. It will take constant time. i.e O(1).

2. for( i=0; i<n; i++ )

  for( j=0; j<n; j++ )

So, There are two loops. One is Outer loop which run on i from 0 to (n-1). i.e n times, And other is inner loop which run from j from 0 to n-1 i.e n time

Value of i Number of times,
inner loop will run for value of i
0 n
1 n
2 n
... ...
... ...
n-1 n

So, here we are seeing that inner loop is running following number of times:

n + n + ... n times =

Here, terms with highest degree is

So, complexity of given code will be

(b)

sum = 0;
for( i=0; i<n; i++ )
for( j=0; j<n*n; j++ )
sum++;

Let's first understand the above code line by line.

1. sum = 0. This is simply an initialization. It will take constant time. i.e O(1).

2. for( i=0; i<n; i++ )

  for( j=0; j<n*n; j++ )

So, There are two loops. One is Outer loop which run on i from 0 to (n-1) i.e n times, And other is inner loop which run from j from 0 to n*n-1 i.e n*n times,

Value of i Number of times,
inner loop will run for value of i
0 n*n
1 n*n
2 n*n
... ...
... ...
n-1 n*n

So, here we are seeing that inner loop is running following number of times:

(n*n) + (n*n) + ... n times =

Here, terms with highest degree is

So, complexity of given code will be

(c)

sum = 0;
for( i=0; i<n; i++ )
for( j=0; j<i; j++ )
sum++;

Let's first understand the above code line by line.

1. sum = 0. This is simply an initialization. It will take constant time. i.e O(1).

2. for( i=0; i<n; i++ )

  for( j=0; j<i; j++ )

So, There are two loops. One is Outer loop which run on i from 0 to (n-1) i.e n times, And other is inner loop which run from j from 0 to i-1 i.e i times,

Value of i Number of times,
inner loop will run for value of i
0 0
1 1
2 2
... ...
... ...
n-1 n-1

So, here we are seeing that inner loop is running following number of times:

0 + 1 + 2 + ... + n-1 =

Here, terms with highest degree is

So, complexity of given code will be

(d)

sum = 0;
for( i=0; i<n; i++ )
for( j=0; j<i*i; j++ )
sum++;

Let's first understand the above code line by line.

1. sum = 0. This is simply an initialization. It will take constant time. i.e O(1).

2. for( i=0; i<n; i++ )

  for( j=0; j<i*i; j++ )

So, There are two loops. One is Outer loop which run on i from 0 to (n-1) i.e n times, And other is inner loop which run from j from 0 to i*i-1 i.e i*i times,

Value of i Number of times,
inner loop will run for value of i
0 0
1 1*1= 1
2 2*2=4
... ...
... ...
n-1 (n-1)*(n-1)

So, here we are seeing that inner loop is running following number of times:

Above came from formulae of sum of square of first n-1 natutal numbers.

Here, when we will multiply and expand above,  terms with highest degree will be

So, complexity of given code will be


Related Solutions

Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
For the following program fragment, (1) give an analysis of the running time T(n) as a...
For the following program fragment, (1) give an analysis of the running time T(n) as a function of n and (2) give a big-O bound on the asymptotic running time (tight bounds for full credit). Sum = 0; for (i=0; i< n; i++)    for(j=0; j < i*i; j++) if(j % i == 0) for (k=0; k<j; k++) Sum = Sum + 1;
Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation...
Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation in terms of the variable n. Show your work a) O( ) int m1(int n, int m) { if (n < 10) return n; else if (n < 100) return happy (n - 2, m); else return happy (n/2, m); } ----------------------------- b) O ( ) void m2 (int n) { j = 0; while (j < n) { for (int i = 0;...
A mid-distance running coach claims that his six-month training program significantly reduces the average time to...
A mid-distance running coach claims that his six-month training program significantly reduces the average time to complete a 1500-meter run. Five mid-distance runners were randomly selected before they were trained with the coach’s six-month training program and their completion time of 1500-meter run was recorded (in minutes). After six-months of training under the coach, the same five runner’s 1500 meter run time recorded again, the results are given below. Runner 1 2 3 4 5 Completion time before training 5.9...
2 algorithms for Prefix Averages, one that ran in big-Oh n2 time and a second that...
2 algorithms for Prefix Averages, one that ran in big-Oh n2 time and a second that ran in big-Oh n time. Code up methods for both algorithms. Show through different input examples and using the Current Time method call how the polynomial time algorithm runs slower than the linear time version. Use system.out.println statement to show your results. please who can help with this question In Java Thank you
Purpose This project is meant to give you experience with sorting, binary searching, and Big-Oh complexity....
Purpose This project is meant to give you experience with sorting, binary searching, and Big-Oh complexity. Objective "Write in Java please" Your goal is to take a book, as a large text file, and build a digital “concordance”. A concordance is like an index for a book in that it contains entries for words in the book, but it also includes passages from the book containing that word. For example, a query for the word Dormouse in Alice in Wonderland...
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for...
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for the following code: Part a       (hint: make use of sum of geometric progression): for (int i=1; i <= n ; i = i*2) {           for ( j = 1 ; j <= i ; j ++)             {                       cout<<”*”;             } } Part b      (hint: make use of sum of square of a number sequence): for (int i=1; i <= n ; i...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of your implementation is O(m)
We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
(a) Explain the difference between big-O and big-theta, and give examples of each to show the...
(a) Explain the difference between big-O and big-theta, and give examples of each to show the difference. (b) How can we say that two functions have the same asymptotic complexity, using big-theta notation? (c) Rank the following functions in order of increasing complexity (rate of growth), and indicate which functions have the same asymptotic complexity. x2; x log(x); 2x; log(x) + 7; 92x2 + 57x + 3921; 4x; 27x2 + 8x3; 22x+5; log(x42); 3x + 12.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT