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]; } } }
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...
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.
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...
A clinical documentation improvement program has been up and running for six months. Initial training of...
A clinical documentation improvement program has been up and running for six months. Initial training of the CDI staff covered the following: • Overview of CDI program and goals • MS-DRGs including CCs and MCCs and their impact on MS-DRG assignment • The top 10 MS-DRGs for the organization • What documentation is used for code assignment and where to find in the paper and electronic medical record • Review of clinical indicators for specific diagnosis such as respiratory failure...
(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.
Give the hexapeptide sequence that gives the following fragments on limited acid hydrolysis: Use the single...
Give the hexapeptide sequence that gives the following fragments on limited acid hydrolysis: Use the single letter abbreviations for amino acids. Fragment 1: Pro-Leu-Gly Fragment 2: Arg-Pro Fragment 3: Gly-Ile-Val Hexapeptide:
Calculate [OH -] and pH for each of the following solutions. (a) 0.0034 M RbOH [OH-]...
Calculate [OH -] and pH for each of the following solutions. (a) 0.0034 M RbOH [OH-] =_________ M pH = _________ (b) 0.0872 g of KOH in 510.0 mL of solution [OH -] =__________ M pH = __________ (c) 79.8 mL of 0.00719 M Sr(OH)2 diluted to 900 mL [OH -] = __________ M pH = ____________ (d) A solution formed by mixing 64.0 mL of 0.000880 M Sr(OH)2 with 36.0 mL of 2.6 x 10-3 M RbOH [OH -]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT