Question

Analyze each line of this brute force code and show that it is Θ(n3)?Please for each...

Analyze each line of this brute force code and show that it is Θ(n3)?Please for each line.. especially the inner for loops.

void bruteForce_n3(int A[], int N, int& bestStart, int& bestEnd, int& bestSum) {
   int current = 0; //to hold current sum with every iteration.
   bestSum = INT_MIN;
   bestStart = 0;
   bestEnd = 0;
   for (int i = 0; i < N; i++) {
       for (int j = i; j < N; j++) {
           current = 0;
           for (int k = i; k <= j; k++)
               current += A[k];
           if (current >= bestSum) {
                   bestSum = current;
                   bestStart = i;
                   bestEnd = j;
           }
       }
   }
}

Homework Answers

Answer #1

Lines above for loop will take constant amount od time. It is clearly seen from the algo. most outer for loop will run 'n' times while the inner loop will depend on the value of i. So it will run as

N times + (N-1) times + (N-2) times +..+ 1 time = N(N+1)/2 = O(N^2).

Now, the most inner loop is depending on the value of i and j. So, for i=0, j will go through 0 to N-1; and k will run 1 times+ 2 times +..+N-1 times = O(N^2) times.

Similarly, for i=1, j will go through 1 to N-1 times. So, k will run 1 times+ 2 times +..+N-1 times = O(N^2) times.

And it will continue upto N values. So, in overall results in O(N^3). And statements after loop will take constant amount of time.

So, in overall time complexity of given algo. will be 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
Show that the running time for the following segment of code is O(N3) without using the...
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++;
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 =...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. for ( int i = n; i > 0; i /= 2 ) { for ( int j = 1; j < n; j += 2 ) { for ( int k = 0; k < n; k += 2 ) { ... // constant number of operations } } }
(Java) For the following code segment, match the different values of the empty line with the...
(Java) For the following code segment, match the different values of the empty line with the resulting Big O complexity of the algorithm. int n = int k = 0; for (int i=0; i <= n; i++){ int j = i; while (j > 0) { // ___MISSING CODE___ k++; } } j = 0; j--; j /= 2; j = j * 2; Options for each are: O(n^3)            O(n * log n)            an...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
Analysing Algorithmic Efficiency (Marks: 3) Analyze the following code fragment and provide an asymptotic (Θ) bound...
Analysing Algorithmic Efficiency (Marks: 3) Analyze the following code fragment and provide an asymptotic (Θ) bound on the running time as a function of n. You do not need to give a formal proof, but you should justify your answer. 1: foo ← 0 2: for i ← 0 to 2n 2 do 3: foo ← foo × 4 4: for j ← 1396 to 2020 do 5: for k ← 4i to 6i do 6: foo ← foo ×...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
PLEASE EXPLAIN EACH LINE OF CODE ONLY. USE COMMENTS NEXT TO EACH LINE TO EXPLAIN WHAT...
PLEASE EXPLAIN EACH LINE OF CODE ONLY. USE COMMENTS NEXT TO EACH LINE TO EXPLAIN WHAT EACH IS CODE IS DOING THANKS. PLEASE COPY PASTE AFTER YOURE DONE NOT SCREENSHOT BECAUSE I NEED TO BE ABLE TO EDIT IT THANKS. // MODULE B: Method 2: Embedding an in-line asssembly language module in a C progrmming #include "stdafx.h" #include "stdio.h" #include<iostream> int main () { printf(" Lab_No_01_Getting_Stated_into_x86_Assembly_from_a_C++_program\n"); // Lab number and title here printf(" Module B: Embedding an in-line asssembly language...
Can someone please tell me what each line of this Java code does exactly? LINE BY...
Can someone please tell me what each line of this Java code does exactly? LINE BY LINE PLEASE!!! package p2; public class Course {    // class fields private String code; // course code, such as "EE333" private String name; // course name, such as "Engineering Programming w/ Objects" public Note notes[]; // attached note of a course private int noteSize; // size of the note by lines    // construct a Course with a code and a name, i.e....
This is my code and can you please tell me why it's not working? By the...
This is my code and can you please tell me why it's not working? By the way, it will work if I reduce 10,000,000 to 1,000,000. #include <iostream> using namespace std; void radixSort(int*a, int n) { int intBitSize = sizeof(int)<<3; int radix = 256; int mask = radix-1; int maskBitLength = 8;    int *result = new int[n](); int *buckets = new int[radix](); int *startIndex = new int[radix]();    int flag = 0; int key = 0; bool hasNeg =...