How do I easily find time complexities for code

Viewed 16

I can find time complexities for general algorithms just by looking at the code, but for more "dynamic" code I sometimes write, I can't figure them out. One example is :

while (found == false) {
    if (pos == goal) {
        found = true ;
    }
    else if (var1 > pos) {
        pos++ ;
        ans++ ;
    }
    else if (var1 < pos) {
        pos-- ;
        ans++ ;
    }
    else {
        var1 = (var1-start)*(-2) + start ;
    }
}

For a Lost Cow Problem Simulation.

Is there a general method for finding the exact Big O notation for these?

0 Answers
Related