So guys, I've already asked a question about how to develop an algorithm here.
The reviewed code looks like this: (note that I've put the elements in the vector L all equal in order to maximize the iterations of the program)
L = [2 2 2 2 2 2 2 2 2];
N = 3;
sumToN = [0 0];
Ret = [0 0];
k = 0;
for i=1:numel(L)-1;
for j=i+1:numel(L);
if L(i)+L(j) == N
sumToN = [L(i) L(j)];
display(sumToN);
return
end
k=k+1
end
end
display(sumToN);
The k
variable is used to keep count of the iterations. The function that counts the number of steps of the algorithm is (1/2)(x-1)x
, with x
being equal to the number of elements in the vector L
. The problem is that the exercise asks me to ensure that the algorithm completes in at most c*numel(L)
for some positive constant c
that does not depend on L
. Moreover, I need to explain why this implementation completes in at most c*length
steps.
How can I do it?