Funksjoner med asymptotisk notasjon

When we use asymptotic notation to express the rate of growth of an algorithm's running time in terms of the input size n n , it's good to bear a few things in mind.
Let's start with something easy. Suppose that an algorithm took a constant amount of time, regardless of the input size. For example, if you were given an array that is already sorted into increasing order and you had to find the minimum element, it would take constant time, since the minimum element must be at index 0. Since we like to use a function of n n in asymptotic notation, you could say that this algorithm runs in Θ(n0) \Theta(n^0) time. Hvorfor? Because n0=1 n^0 = 1 , and the algorithm's running time is within some constant factor of 1. In practice, we don't write Θ(n0) \Theta(n^0) , however; we write Θ(1) \Theta(1) .
Now suppose an algorithm took Θ(log10n) \Theta(\log_{10} n) time. You could also say that it took Θ(log2n) \Theta(\log_2 n) time. Whenever the base of the logarithm is a constant, it doesn't matter what base we use in asymptotic notation. Why not? Because there's a mathematical formula that says
logan=logbnlogba \displaystyle \log_a n = \dfrac{\log_b n}{\log_b a}
for all positive numbers a a , b b , and n n . Therefore, if a a and b b are constants, then logan \log_a n and logbn \log_b n differ only by a factor of logba \log_b a , and that's a constant factor which we can ignore in asymptotic notation.
Therefore, we can say that the worst-case running time of binary search is Θ(logan) \Theta(\log_a n) for any positive constant a a . Why? The number of guesses is at most log2n+1 \log_2 n + 1 , generating and testing each guess takes constant time, and setting up and returning take constant time. However, as a matter of practice, we often write that binary search takes Θ(log2n) \Theta(\log_2 n) time because computer scientists like to think in powers of 2.
There is an order to the functions that we often see when we analyze algorithms using asymptotic notation. If a a and b b are constants and a<b a < b , then a running time of Θ(na) \Theta(n^a) grows more slowly than a running time of Θ(nb) \Theta(n^b) . For example, a running time of Θ(n) \Theta(n) , which is Θ(n1) \Theta(n^1) , grows more slowly than a running time of Θ(n2) \Theta(n^2) . The exponents don't have to be integers, either. For example, a running time of Θ(n2) \Theta(n^2) grows more slowly than a running time of Θ(n2n) \Theta(n^2 \sqrt{n}) , which is Θ(n2.5) \Theta(n^{2.5}) .
The following graph compares the growth of nn,n2n^2, and n2.5n^{2.5}:
Logarithms grow more slowly than polynomials. That is, Θ(log2n) \Theta(\log_2 n) grows more slowly than Θ(na) \Theta(n^a) for any positive constant a a . But since the value of log2n \log_2 n increases as n n increases, Θ(log2n) \Theta(\log_2 n) grows faster than Θ(1) \Theta(1) .
The following graph compares the growth of 11, nn, and log2n\log_2 n:
Here's a list of functions in asymptotic notation that we often encounter when analyzing algorithms, ordered by slowest to fastest growing:
  1. Θ(1) \Theta(1)
  2. Θ(log2n) \Theta(\log_2 n)
  3. Θ(n) \Theta(n)
  4. Θ(nlog2n) \Theta(n \log_2 n)
  5. Θ(n2) \Theta(n^2)
  6. Θ(n2log2n) \Theta(n^2 \log_2 n)
  7. Θ(n3) \Theta(n^3)
  8. Θ(2n) \Theta(2^n)
  9. Θ(n!) \Theta(n!)
Note that an exponential function ana^n, where a>1a > 1, grows faster than any polynomial function nb n^b , where b b is any constant.
The list above is not exhaustive, there are many functions with running times not listed there. You'll hopefully run into a few of those in your computer science journey.

This content is a collaboration of Dartmouth Computer Science professors Thomas Cormen and Devin Balkcom, plus the Khan Academy computing curriculum team. The content is licensed CC-BY-NC-SA.
Laster