Friday, October 7, 2016

Big O notation, algorithm time complexity and space complexity


The algorithm complexity is expressed by a Big O notation, like these
O(1)
O(log2n)
O(n)
O(nlog2n)
O(n2)
O(n3)
...
O(2n)
...
The time complexity sequence is 
Ο(1)<Ο(log2n)<Ο(n)<Ο(nlog2n)<Ο(n2)<Ο(n3)<…<Ο(2n)<Ο(n!)

Basically, it describes the worst iteration times.  For example, the code below


So the time complexity is O(2n2 + n + 1)
The big-O expressions do not have constants or low-order terms. This is because, when N gets large enough, constants and low-order terms don't matter.

  • Remove constants, which is 1. Now it is O(2n2 + n)
  • Remove low-order terms, which is n. Now it is O(2n2)
  • Remove constant factor of high-order terms, which is 2 here. Then it is O(n2)


The time complexity is O(n2), because the inner iteration time is n*n.


Here is the article http://blog.csdn.net/zolalad/article/details/11848739

Another article https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

No comments:

Post a Comment