0

Hi,

I'm working on my time complexity understanding in C++. Right now, I'm trying to figure it out, and kinda got some of them, such as:
constant O(1):

int first(int N){
	int i = N;
	int x = 0;
	x++;

	return x;
}

O(n)

int second(int N){
 int sum = 0,
     i = 0;
 const int n = N;
 while(i < n){
    sum += i;
    i++;
 }
 return i;
}

and O(n^2)

int third(int N){
    int count = 0,
        count1 = 0,
        count2 = 0,
        n = N;
    for(count1 = 1; count1 <= n; count1++){
        for(count2 = 1; count2 <= n; count2++){
            int i = 0;
            i++;
        }
    }
    count = count1 + count2;
    return count;
}

These might be wrong, since I'm a little confused about all this.

But, I'm totally not sure about the O(n^3) (do I need 3 loops???), O(log n), and O(n log n) (this one is just throwing me off)...

Any help would be appreciated :)

Thanks

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Narue
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.