0

I am just trying to figure out why

3^2500 < log(n) < 5log(n) < nlog(n^2) < nlog(n) < n^270

this is not true. This is from fastest to slowest.

Could anyone tell me why this is wrong?

I am confused with the middle part 5log(n) < nlog(n^2) < nlog(n)

Don't they share the same order of magnitude?

One more question,

sum = 0
 for i in myIntegerList:
   if i > 0:
     sum += i

For this code fragment, the best case and the worst case would be O (n) right?

I am thinking that way because for loop will have a magnitude of n regardless

how many element myIntegerList contains? Also, there is no operation or

statement that results in the ending of the code.

krnactry
  • 57
  • 1
  • 6

2 Answers2

1

Note:

  • 3^2500 is O(1)
  • log(n) is O(log n)
  • 5log(n) is O(log n) as well, but a higher constant
  • nlog(n²) is O(n·log n²) = O(2·n·log n) = O(n·log n)
  • nlog(n) is O(n·log n) as well, but a smaller constant
  • n^270 is O(n²⁷⁰)

So correct order is:

O(3^2500) < O(log(n)) < O(5·log(n)) < O(n·log(n)) < O(n·log(n^2)) < O(n^270)

Note it isn't correct to use comparisons in these cases without big O notation.

The summation is O(n), n being the length of the list, in all cases.

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
1

I am confused with the middle part 5log(n) < nlog(n^2) < nlog(n)

Don't they share the same order of magnitude?

Considering n is very large :

log(n^2) > log(n).

5log(n) ~ log(n)

nlog(n) > 5log(n) because 5 is a constant co-efficient.

thus the right answer is:

13^2500 < log(n) < 5log(n) < nlog(n) < nlog(n^2) < n^27

For this code fragment, the best case and the worst case would be O (n) right?

Yes. You iterating over all the elements in myIntegerList. Where n is the number of elements in myIntegerList.

I am thinking that way because for loop will have a magnitude of n regardless

how many element myIntegerList contains?

Wrong. It depends on the number of elements myIntegerList contains. Also, the for loop iterates over all elements in myIntegerList and stops after it is done iterating. The program will automatically end after the completion of the for loop.

Viraj
  • 777
  • 1
  • 13
  • 32