Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Big O notation, how do you factor in calls to other methods?

Tags:

c++

big-o

Hypothetically, lets say we have these two methods:

void example(int p){
    p += 10;
    p = exampleTwo(p);
}

int exampleTwo(int p){
    int pp = 0;
    for(int i = 0; i < p; i++){
        pp++;
    }
    return pp;
}

The method exampleTwo has a linear runtime. Its run time is O(n).

So, what is the big O notation of the method example, taking into account that it calls exampleTwo?

I would imagine it is also O(n), but I do not know for sure.

like image 691
user2726232 Avatar asked Oct 20 '25 15:10

user2726232


1 Answers

For subroutines, you should multiply the order by the order of the number of the number of times it is called. For example, if a function is called O(n) times, and runs in O(log n) time, the total order is O(n log n).

like image 184
Dan Avatar answered Oct 23 '25 04:10

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!