dark

# What is the Big O notation, and how to evaluate the time complexity of an algorithm

The Big O notation is a way to describe the upper bound of the time complexity of an algorithm. It describes the worst-case scenario, or the maximum amount of time an algorithm would take to solve a problem of a given size. For example, if an algorithm has a time complexity of O(n), it means that the time it takes to solve a problem doubles as the size of the problem doubles.

To evaluate the time complexity of an algorithm, you can look at the operations that are performed most frequently, and count how many times they are performed in relation to the size of the input. The operation that is performed most frequently will give you the highest-order term in the time complexity of the algorithm. For example, if an algorithm involves a nested loop, where the outer loop runs n times, and the inner loop runs m times for each iteration of the outer loop, the time complexity of the algorithm would be O(n*m).

It’s important to note that the Big O notation is an approximation of the time complexity and not the exact time complexity of an algorithm. And, it’s only useful for comparing the relative performance of different algorithms.

## Examples of evaluating the time complexity of a simple algorithm

Here are examples of evaluating the time complexity of a simple algorithm:

### Example 1

``````def sum_of_squares(n):
sum = 0
for i in range(1, n+1):
sum += i*i
return sum
``````

This algorithm calculates the sum of squares of the first n natural numbers. To evaluate its time complexity, we need to count the number of basic operations performed by the algorithm in relation to the size of the input. In this case, the only operation performed is the addition of i*i to the sum, which is done n times. Therefore, the time complexity of this algorithm is O(n).

### Example 2

``````def find_element(arr, element):
for i in range(len(arr)):
if arr[i] == element:
return i
return -1
``````

This algorithm takes an array `arr` and an `element` as input and returns the index of the element in the array if it exists, otherwise returns -1. Here, we have one basic operation which is the comparison of arr[i] with `element` and it is performed n times where n is the size of the array. So, the time complexity of this algorithm is O(n).

As you can see, by counting the number of basic operations performed by the algorithm and how they relate to the size of the input, we can determine the time complexity of an algorithm using the Big O notation.

## Does the programing language used, affect the big o notation of an algorithm

The programming language used can affect the performance of an algorithm, but it does not directly affect the Big O notation of the algorithm. The Big O notation describes the upper bound of the time complexity of an algorithm, regardless of the programming language or the specific implementation used.

For example, the time complexity of a given algorithm in Python may be slower than the same algorithm implemented in C++ because of the overhead of the Python interpreter. However, the Big O notation of the algorithm would be the same in both languages.

That being said, some programming languages may have built-in data structures or libraries that can be used to implement an algorithm more efficiently. So, the performance of an algorithm can be affected by the programming language used, but the Big O notation will remain the same.

It’s also important to note that the Big O notation is an approximation of the time complexity and not the exact time complexity of an algorithm, and it’s only useful for comparing the relative performance of different algorithms.

##### Previous Post ## Python 3 tutorial series outline

##### Next Post ## Getting started with Python 3

##### Related Posts ## system_engineering-devops part 2

What do the commands chmod, sudo, su, chown, chgrp do Linux file permissions How to represent each of the three sets of permissions (owner, group, and other) as a single digit ## alx-low_level_programming/0x06-pointers_arrays_strings 