How to determine algorithm complexity?
I can't seem to understand how to determine the complexity of algorithms.
For example:
for j=n:-1:1
for i=j+1:n
x(i,j)=0
x(j,j)=b(j,j)/c(j,j)
for i=j-1:-1:1
x(i,j)=(b(i,j)-c(i,i+1)*x(i+1,j))/c(开发者_如何学运维i,i)
This is more of a math problem, but still.
I use simple sum formulas and I find the result to be 2·n2 but it seems the correct result is 5·n2/2.
Can someone please help me understand the correct way of calculating this?
Assuming you are writing in MATLAB, the complexity is O(n^2). Observe that you are evaluating the sum:
sum_{j=1 to n} j + (n - j) = sum_{j=1 to n) n = n^2
精彩评论