Perform a worst-case analysis of each of the following fragments and give a ? bound for the running time:
a. sum = 0;
for (int i = 0; i < N; i++)
for (int j = i; j ? 0; j--)
sum++;
My confusion here is how do i count the number of iterations in a for loop and the running time.
what i did:
i calculated the Running time of each line
1) sum = 0 is O(1)
2) For... O(N)
3) ?
4) O(1)
I am guessing i should add them up and try to get the running time but I need someone to explain to me how to solve such problem please.
a. sum = 0;
for (int i = 0; i < N; i++)
for (int j = i; j ? 0; j--)
sum++;
My confusion here is how do i count the number of iterations in a for loop and the running time.
what i did:
i calculated the Running time of each line
1) sum = 0 is O(1)
2) For... O(N)
3) ?
4) O(1)
I am guessing i should add them up and try to get the running time but I need someone to explain to me how to solve such problem please.