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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.