Functions in asymptotic notation

When we use asymptotic notation to express the rate of growth of an algorithm's running time in terms of the input size

n

{\displaystyle 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

{\displaystyle n}

 in asymptotic notation, you could say that this algorithm runs in

Θ
(

n
0

)

{\displaystyle \Theta(n^0)}

 time. Why? Because

n
0

=
1

{\displaystyle n^0 = 1}

, and the algorithm's running time is within some constant factor of 1. In practice, we don't write

Θ
(

n
0

)

{\displaystyle \Theta(n^0)}

, however; we write

Θ
(
1
)

{\displaystyle \Theta(1)}

. Now suppose an algorithm took

Θ
(

log

10

⁡
n
)

{\displaystyle \Theta(\log\_{10} n)}

 time. You could also say that it took

Θ
(

log
2

⁡
n
)

{\displaystyle \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

log
a

⁡
# n

log
b

⁡
n

log
b

⁡
a

{\displaystyle \log\_a n = \dfrac{\log\_b n}{\log\_b a}}

for all positive numbers

a

{\displaystyle a}

,

b

{\displaystyle b}

, and

n

{\displaystyle n}

. Therefore, if

a

{\displaystyle a}

 and

b

{\displaystyle b}

 are constants, then

log
a

⁡
n

{\displaystyle \log\_a n}

 and

log
b

⁡
n

{\displaystyle \log\_b n}

 differ only by a factor of

log
b

⁡
a

{\displaystyle \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

Θ
(

log
a

⁡
n
)

{\displaystyle \Theta(\log\_a n)}

 for any positive constant

a

{\displaystyle a}

. Why? The number of guesses is at most

log
2

⁡
n
1

{\displaystyle \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

Θ
(

log
2

⁡
n
)

{\displaystyle \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

{\displaystyle a}

 and

b

{\displaystyle b}

 are constants and

a
<
b

{\displaystyle a < b}

, then a running time of

Θ
(

n
a

)

{\displaystyle \Theta(n^a)}

 grows more slowly than a running time of

Θ
(

n
b

)

{\displaystyle \Theta(n^b)}

. For example, a running time of

Θ
(
n
)

{\displaystyle \Theta(n)}

, which is

Θ
(

n
1

)

{\displaystyle \Theta(n^1)}

, grows more slowly than a running time of

Θ
(

n
2

)

{\displaystyle \Theta(n^2)}

. The exponents don't have to be integers, either. For example, a running time of

Θ
(

n
2

)

{\displaystyle \Theta(n^2)}

 grows more slowly than a running time of

Θ
(

n
2

n

)

{\displaystyle \Theta(n^2 \sqrt{n})}

, which is

Θ
(

n

2.5

)

{\displaystyle \Theta(n^{2.5})}

. The following graph compares the growth of

n

{\displaystyle n}

,

n
2

{\displaystyle n^2}

, and

n

2.5

{\displaystyle n^{2.5}}

:

(Missing image)

Logarithms grow more slowly than polynomials. That is,

Θ
(

log
2

⁡
n
)

{\displaystyle \Theta(\log\_2 n)}

 grows more slowly than

Θ
(

n
a

)

{\displaystyle \Theta(n^a)}

 for any positive constant

a

{\displaystyle a}

. But since the value of

log
2

⁡
n

{\displaystyle \log\_2 n}

 increases as

n

{\displaystyle n}

 increases,

Θ
(

log
2

⁡
n
)

{\displaystyle \Theta(\log\_2 n)}

 grows faster than

Θ
(
)

{\displaystyle \Theta()}

.
The following graph compares the growth of

1

{\displaystyle 1}

,

n

{\displaystyle n}

, and

log
2

⁡
n

{\displaystyle \log\_2 n}

:

(Missing image)

Here's a list of functions in asymptotic notation that we often encounter when analyzing algorithms, ordered by slowest to fastest growing:

1. Θ
(
1
)

{\displaystyle \Theta(1)}
2. Θ
(

log
2

⁡
n
)

{\displaystyle \Theta(\log\_2 n)}
3. Θ
(
n
)

{\displaystyle \Theta(n)}
4. Θ
(
n

log
2

⁡
n
)

{\displaystyle \Theta(n \log\_2 n)}
5. Θ
(

n
2

)

{\displaystyle \Theta(n^2)}
6. Θ
(

n
2

log
2

⁡
n
)

{\displaystyle \Theta(n^2 \log\_2 n)}
7. Θ
(

n
3

)

{\displaystyle \Theta(n^3)}
8. Θ
(

2
n

)

{\displaystyle \Theta(2^n)}
9. Θ
(
n
!
)

{\displaystyle \Theta(n!)}

Note that an exponential function

a
n

{\displaystyle a^n}

, where

a
>
1

{\displaystyle a>1}

 grows faster than any polynomial function

n
b

{\displaystyle n^b}

 where

b

{\displaystyle 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.