2.3. Big-O Notation¶
When trying to characterize an algorithm’s efficiency in terms of execution time, independent of any particular program or computer, it is important to quantify the number of operations or steps that the algorithm will require. If each of these steps is considered to be a basic unit of computation, then the execution time for an algorithm can be expressed as the number of steps required to solve the problem. Deciding on an appropriate basic unit of computation can be a complicated problem and will depend on how the algorithm is implemented.
A good basic unit of computation for comparing the summation algorithms
shown earlier might be to count the number of assignment statements
performed to compute the sum. In the function sumOfN
, the number of
assignment statements is 1 (
In the summation functions given above, it makes sense to use the number of terms in the summation to denote the size of the problem. We can then say that the sum of the first 100,000 integers is a bigger instance of the summation problem than the sum of the first 1,000. Because of this, it might seem reasonable that the time required to solve the larger case would be greater than for the smaller case. Our goal then is to show how the algorithm’s execution time changes with respect to the size of the problem.
Computer scientists prefer to take this analysis technique one step
further. It turns out that the exact number of operations is not as
important as determining the most dominant part of the
In the above example,
As another example, suppose that for some algorithm, the exact number of
steps is
- O(2n)
- No, 3n2 dominates 2n. Try again.
- O(n)
- No, n2 dominates n. Try again.
- O(3n2)
- No, the 3 should be omitted because n2 dominates.
- O(n2)
- Right!
- More than one of the above
- No, only one of them is correct. Try again.
Q-1: If the exact number of steps is
Although we do not see this in the summation example, sometimes the performance of an algorithm depends on the exact values of the data rather than simply the size of the problem. For these kinds of algorithms we need to characterize their performance in terms of best case, worst case, or average case performance. The worst-case performance refers to a particular data set where the algorithm performs extremely poorly. At the same time, a different data set for the exact same algorithm might have outstanding performance. However, in most cases the algorithm performs somewhere in between these two extremes (average case). It is important for a computer scientist to understand these distinctions so they are not misled by one particular case.
A number of very common order of magnitude functions will come up over
and over as you study algorithms. These are shown in Table 1. In
order to decide which of these functions is the dominant part of any
f(n) |
Name |
---|---|
Constant |
|
Logarithmic |
|
Linear |
|
Log Linear |
|
Quadratic |
|
Cubic |
|
Exponential |
Figure 1 shows graphs of the common functions from Table 1. Notice that when n is small, the functions are not very well defined with respect to one another. It is hard to tell which is dominant. However, as n grows, there is a definite relationship and it is easy to see how they compare with one another.

Figure 1: Common Big-O Functions¶
Without looking at the graph above, from top to bottom order the following from most to least efficient.
As a final example, suppose that we have the fragment of C++ code shown in Listing 2. Although this program does not really do anything, it is instructive to see how we can take actual code and analyze performance.
Listing 2
C++ Implementation
#include <iostream>
using namespace std;
int main(){
int a=5;
int b=6;
int c=10;
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
int x = i * i;
int y = j * j;
int z = i * j;
}
}
for (int k = 0; k < n; k++){
int w = a*k + 45;
int v = b*b;
}
int d = 33;
return 0;
}
Python Implementation
def main():
a=5
b=6
c=10
for i in range(n):
for j in range(n):
x = i * i
y = j * j
z = i * j
for k in range(n):
w = a*k + 45
v = b*b
d = 33
main()
The number of assignment operations is the sum of four terms. The first
term is the constant 3, representing the three assignment statements at
the start of the fragment. The second term is

Figure 2: Comparing
Figure 2 shows a few of the common Big-O functions as they
compare with the
- Algorithm 1 will require a greater number of steps to complete than Algorithm 2
- This could be true depending on the input, but consider the broader picture
- Algorithm 2 will require a greater number of steps to complete than Algorithm 1
- This could be true depending on the input, but consider the broader picture
- Algorithm 1 will require a greater number of steps to complete than Algorithm 2 until they reach the crossover point
- Correct!
- Algorithm 1 and 2 will always require the same number of steps to complete
- No, the efficiency of both will depend on the input
Q-3: Which of the following statements is true about the two algorithms? Algorithm 1: 100n + 1 Algorithm 2: n^2 + n + 1
Self Check
- 3.444
- Incorrect. Try again.
- 2.53
- Correct!
- 2
- Incorrect. Try again.
- 4
- Incorrect. Try again.
- More than one of the above
- No, only one of them is correct. Try again.
Q-4: The Big O of a particular algorithm is
- 3.444
- Incorrect. Try again.
- 2.53
- Incorrect. Try again.
- 2
- Incorrect. Try again.
- 4.2
- Right!
- More than one of the above
- No, only one of them is correct. Try again.
Q-5: The Big O of a particular algorithm is
- 2000
- Incorrect. Try again. Think about what happens to the time as more operations occur.
- 3000
- Incorrect. Try again. Think about what happens to the time as more operations occur.
- 16
- Correct!
- 1500
- Incorrect. Try again. Think about what happens to the time as more operations occur.
- More than one of the above
- No, only one of them is correct. Try again.
Q-6: The Big O of a particular algorithm is
- 2000
- Right!
- 3000
- Incorrect. Try again. Think about what happens to the time as more operations occur.
- 16
- Incorrect. Try again. Think about what happens to the time as more operations occur.
- 1500
- Incorrect. Try again. Think about what happens to the time as more operations occur.
- More than one of the above
- No, only one of them is correct. Try again.
Q-7: The Big O of a particular algorithm is