1
$\begingroup$

Given Algorithm:

 for i = 1, 2, . . . , n  
     for j = i + 1, i + 2, . . . , n
         Add up array entries A[i] through A[j]
         Store the result in B[i, j]
    Endfor
 Endfor

My attempt: Solving for worst case using Big-O notation:

$$\sum_{i=1}^{n} \sum_{j=i+1}^{n}n = \sum_{i=1}^{n}n(n-i) = \frac{n^{3}}{2}-\frac{n^{2}}{2}$$ Therefore for the worst case running time we have: $$O(n^3)$$ Is that correct? Or did I not interpreted the algorithm correctly.

  • 0
    \begin{eqnarray*} B(i,j)= \sum_{k=i}^{j} A(k) \end{eqnarray*}2017-02-21
  • 3
    Yeah it will have a run time that is order $n^3$.2017-02-21
  • 2
    worst case, first loop is linear, so is 2nd loop, so is the addition inside - hence $n \cdot n \cdot n = n^3$...2017-02-21
  • 0
    Ah I see, thanks.2017-02-21

0 Answers 0