What would be the runtime complexity of this piece of code. The code works as it is supposed to work, I am just a little confused about the runtime complexity.
int Something(int x[]){
int i=0;
for(i=0;i<x.length;i++){
//some code over here
i=-1;
}
Please be aware this is not an infinite loop since there is a continue and break statement in the loop. However it does loop quite a few times because of the condition i = -1 at the end of the loop.
O(n) complexity means that there are no nested loops and this code has no nested loops. But I don’t really think this will be O(n). It also won’t be O(n^2) or anything like that since there aren’t any nested loops.
In it’s current form this is O(infinity). It might never stop.
If there is a break statement in the loop you have to provide the full code. Without that, analysis is not possible.