I am trying to understand how to compute the time complexity of an algorithm .
I have this piece of code: This is the whole method:
public void solve(int i) {
if(i < 2) {
return;
}
solve(i-1); //recursive call
int x = v[n-i];
for(int j = n-i+1; j < n; j++) {
if(x > v[j]) {
count++;
}
}
return;
}
I think the complexity is O(n). Am I right?
Thanks
The complexity should be
O(N^2)
as you will haveN + (N-1) + (N-2) + 1 = N ( N + 1 ) / 2
iterations in worst case.