procedure matrixvector(n:integer);
var i,j:integer;
begin
for i<-1 to n do begin
B[i] = 0;
C[i] = 0;
for j<-1 to i do
B[i]<- B[i]+ A[i,j];
for j<-n down to i+1 do
C[i]<-C[i] + A[i,j]
end
end;
views:
1151answers:
5O(n^2), if I read it right.
Why you need two inner loops is beyond me. Why not sum B and C in the same loop?
I agree with duffymo on the complexity, but the two loops are needed to do that kind of adding. It wouldn't change anything about the complexity, if it was written somehow differently.
worst case is O(n²).
there are indeed three loops, but not all inside each other, thus giving O(n²).
also, you can clearly see that the inner loops won't go from 1 to n (like the outer loop does). But because this would only change the time complexity by some constant, we can ignore this and say that it is just O(n^2).
This shows that time complexity is a measure saying: your algorithm will scale with this order, and it won't ever take any longer. (faster is however always possible)
for more information about "calculating" the worst case complexity of any algorithm, I can point you to a related question I asked earlier
"needed"? Why not sum both inside one loop? It's not necessary to break them out.