Question

Show that the running time for the following segment of code is O(N3) without using the...

  1. Show that the running time for the following segment of code is O(N3) without using the rule for loop. Make sure to count each statement that takes one unit of time.

sum = 0;

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

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

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

sum++;

Homework Answers

Answer #1
sum = 0;
This line runs for 1 time

for( i = 0; i < n; i++ )
This line runs for n time

for( j = 0; j < n; j++ )
This line runs for n*(n+1) time

for( k = 0; k < n; k++ )
This line runs for n*n*(n+1) time

sum++;
This line runs for n*n*n time

So, time complexity
= 1 + n + n*(n+1) + n*n*(n+1) + n*n*n
= 1 + n + n*n + n + n*n*n + n*n + n*n*n
= 1 + 2*n + 2*n*n + 2*n*n*n
= O(Dominant term in the equation by removing constant multipliers)
= O(n*n*n)
= O(n^3)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code...
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code fragment. Algorithm test int counter, i, j; counter := 0; for (i:= 1; i < n; i*=2) { for (j:= 0; j < i; j++) { counter := counter +1 } } 3)Let f(n) = 2lg 8n + 3 be a function.
Analyze the worst case running time of the following code in “big-Oh” notation in terms of...
Analyze the worst case running time of the following code in “big-Oh” notation in terms of the variable n. a. void fdisp1(int n) { for(int i=0; i < n; i++) { for(int j=0; j < n; j++) { for(int k=0; k < n; k++) { for(int m=0; m < n; m++) { System.out.println("Hi I am Here"); } } } } } b. voidprintAllPossibleOrderedPairs(intarr[], int size) { for (int i = 0; i < size; i++) { for (int j =...
1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will...
1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will accomplish the same as the following: matsum = sum(mat') Please make sure to store the result in "matsum" and dont use sum. This is what I have so far but it keeps telling me is wrong.... % mat has been initialized for you: mat = randi([-100,100],randi([15,20]),randi([15,20])); % write your statements that accomplish the code above: [r c]=size(mat); for i=1:c matsum=0; for j=1:r matsum=matsum+mat(j,i); end...
Using Big O notation, indicate the time requirement of each of the following tasks in the...
Using Big O notation, indicate the time requirement of each of the following tasks in the worst case. Computing the sum of the first n even integers by using a for loop            [ Choose ] O(1) O(2n) O(n*log n ) O(2^n) O(log n) O(n^2) O(n) O(2) O(n^3)          Displaying all n integers in an array            [ Choose ] O(1) O(2n) O(n*log n ) O(2^n) O(log n) O(n^2) O(n) O(2) O(n^3)          Displaying all n integers in a sorted linked chain            [ Choose...
The following algorithm finds the initial substring of y that can be reversed and found in...
The following algorithm finds the initial substring of y that can be reversed and found in y. For example, longestInitialReverseSubstringLength(“aabaa”) = 5, because “aabaa” is the same string forwards and backwards, so the longest initial substring that can be reversed and found in the string is “aabaa”. Also, longestInitialReverseSubstringLength(“bbbbababbabbbbb”) is 6, because “babbbb” can be found in the string (see color-highlighted portions of the string), but no longer initial string exists reversed in any part of the string. longestInitialReverseSubstringLength(String y)...
Show, using supply and demand for labor diagrams for Divisions I and III, the effects of...
Show, using supply and demand for labor diagrams for Divisions I and III, the effects of a new rule allowing Division III schools to pay athletes a one-time bonus for enrolling at their schools. Make sure to label all curves and axes
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.   For example: the maximum subarray sum for the...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Design the following synchronous counters (one at a time) using the 7493 IC. Show all your...
Design the following synchronous counters (one at a time) using the 7493 IC. Show all your connections clearly (use Multisim). Show the connections to pins R0 (1) and R0 (2) so that you can get each of the following: Mod 10 counter, Mod 12 counter and Mod 15 counter. You must use a BCD decoder (74LS47N), resistors, and a seven-segment display