View the step-by-step solution to:

Time complexity Choose the correct answer and say why. a) Given the following program with n as parameter:

Time complexity

Choose the correct answer and say why.

a)     Given the following program with n as parameter:

0                                i = 0; j =0;

1                                for(int k=0; k<=n; k++)

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

3                                    {

4                                        i=i+k;

5                                               for( int k = 0; k < i; k++ )

6                                                      j++;

7                                     }

1. How many times is statement 4 executed?

a. O( N )

b. O( N2 )

c. O( N3 )

d. O( N4 )

e. none of the above


2. How many times is statement 6 executed?

a. O( N )

b. O( N2 )

c. O( N3 )

d. O( N4 )

e. none of the above


3. What is the time complexity of the above program?

a. O( N4 )

b. O( N5 )

c. O( N6 )

d. O( N7 )

e. none of the above


b) Given the following program with n as parameter:

0 i = 0; j =0;

1 while(i<1000)

2 for( int k = i; k <= n; k++ ){

3 i++;

4 j++;}

5 for( int p = 0; p < n*n; p++ )

6 for( int q = 0; q < p; q++ )

7 j--;


1. How many times is the instruction 3 executed?

a. O( N )

b. O( N2 )

c. O( N3 )

d. O( N4 )

e. none of the above


2. How many times is statement 7 executed?

a. O( N )

b. O( N2 )

c. O( N3 )

d. O( N4 )

e. none of the above


3. What is the time complexity of the above program?

a. O( N4 )

b. O( N5 )

c. O( N6 )

d. O( N7 )

e. none of the above

Top Answer

a) Answer1) b. O(N2) Answer2) c. O(N3) Answer3) e.... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online