notatimelord
notatimelord

Reputation: 3

Big O Time Complexity of While Loops

I am a bit confused about the time complexity, in the case of two seperate while loops. I am aware that the code:

while(i<N){
     code
    while(k<N){
       code
    }
}

will have a complexity of O(n^2) What about the case where we don't have nested loops, though?

while(i<N){
}
while(k<N){
}

Upvotes: 0

Views: 174

Answers (1)

Berthur
Berthur

Reputation: 4495

So you run two loops, one after the other. If they both perform n iterations, then your code performs 2n loop iterations in total.

Now, 2n = O(2n) = O(n) (by the properties of big-O notation), so that is your time complexity.

Upvotes: 1

Related Questions