简体   繁体   中英

Big-O time complexity

I have been doing some self-study on Big-O. I understand how to give examples of the following notations to algorithms:

O(N):

for(int i = 0; i < n; i++)
    sum++;

O(N^2):

for(int i = 0; i < n; i++)
    for( int j = 0; j < n; j++)
        sum++;

O(N^3):

for(int i = 0; i < n; i++)
    for( int j = 0; j < n * n; j++)
        sum++;

I have come across these notations which I don't quite comprehend. How do I give examples of these in terms of algorithms?

Maybe I should phrase it this way: write an algorithm which takes running time in proportion to:

  1. O((n^3)/4)
  2. log n^3
  3. O((log^2)n)+O(n)
  4. 4^n
  5. n^3/2

I fear you are misunderstanding "Big-O" notation.

A notation is not "expressed" as an algorithm. Rather, the Big-O notation describes a property of an algorithm.

So it's not "O(N) can be expressed as XXX", but rather "algorithm XXX has complexity of O(N)".

That said, it is quite reasonable to ask for examples of algorithms with a certain complexity; you already list some. To address your questions:

O(4^n) is the same as O(e^n), often written as O(exp(n)) (try to understand why it is the same). O(4^n) belongs to the class of algorithms with "exponential complexity" ( EXPTIME ). Many important problems in math/CS have exponential complexity (or nearly exponential complexity).

An algorithm with exponential complexity would be for example a naive solution to the discrete logarithm problem .

For the other complexities I cannot give an example, but you will probably find one by googling a bit.

The algorithms you have given are strictly speaking not Big-O but Theta. Big-O is an asymptotic upper bound meaning that on some worse case input the running time will be the one given but isn't for all inputs, where as Theta is a tight bound meaning that the running time will always be that. Consider for instance a sorting algorithm that is given an already sorted list as input, or a search algorithm where the things to be found is the first element in a list (how does binarysearch compare to linear search in this case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM