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 =...
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...
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++)...
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 ×...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing...
For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing correctness for the main loop in each of the following programs and briefly argue initialization, preservation, and termination. EXAMPLE PROBLEM: //Function to return the max of an array A Maximum(array of integers A) Local integer integer m m=0 for i = 1 to n if A[i] > m then m = A[i] end function Maximum EXAMPLE SOLUTION: The loop invariant is m = max(0,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT