Big O Note on Some Examples

The professor gave us some examples to try at home, but he did not give us answers, and now, while reviewing the exams, I would really like to know more about this. We have 3 "algorithms", and for these algorithms we need to develop a large O.

The first:

sum <- 0
for i=1 to N do
    for j=1 to i do
        sum = sum + 1

Since loops of this type are very similar to bubbles sorting loops, I came to the conclusion that this is O (n ^ 2).

Second example:

sum <- 0
for i=1 to N do
    for j=1 to i*i do
        for k=1 to j do
            sum = sum + 1

I figured it should be O (n ^ 5).

Third example:

sum <- 0
for i=1 to N do
    for j=1 to i*i do
        if j mod i = 0 then
            for k=1 to j do
                sum = sum + 1

I think it will be O (n ^ 4), because the third inner loop will only start Ni instead of Ni * i. Any comments are welcome. Thanks in advance.

+4
source share

All Articles