4

The professor gave us a few examples to try at home but never gave us the answers and now when revising for the exams I would really like to go a bit more into detail with this. We have 3 "algorithms" and we have to work out the big O for these algorithms.

1st one:

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

Since the for loops of this look a lot like the bubble sort loops I came to the conclusion it is O(n^2).

2nd 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 estimated this should be O(n^5).

3rd 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 this one would be O(n^4) because the third inner loop will run only Ni instead of Ni*i. Any comments are appreciated. Thanks in advance.

Elldorin
  • 69
  • 5

0 Answers0