big O notation check

So I've been trying to understand this big o notation thing. I've read about it and watched videos on it However since I'm very new to this stuff I just want to make sure I understand.


Do I have the right idea?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main() {
    
    int foo[10] = {10,1,9,2,3,4,5,8,7,6};
    
    cout << foo[2]; // O(1) constant time?
    
    
    for (int i = 0; i < sizeof(foo)/sizeof(*foo); i++){ // O(n) linear time?
        cout << i;
    }
    
    int moo[10] = {20,29,28,27,21,32,45,65};
    
    
    for (int i = 0; i < sizeof(foo)/sizeof(*foo); i++){ // O(n)2 (square) ?? time?
        for (int j = 0; j < sizeof(moo)/sizeof(*moo); j++){
            cout << moo[j] + foo[j];
            }
}


    
}


Last edited on
Well it's O(bad) now because on line 21 it should be foo[i], not foo[j]. :)

The loop from lines 19-22 is more accurately O(n*m) where n is the size of foo and m is the size of moo. If you're guaranteed that these are the same size N then yet, it's O(N2)
Topic archived. No new replies allowed.