Big O in Simple Words

Palistha
3 min readApr 14, 2022

--

There are many programmers in the world. And everyone writes the code in their way. However, let’s talk about two ways of writing code.

  • Good way of writing code
  • Bad way of writing code

Now, the question is how do we distinguish between good and bad code. How do we know which code is more efficient?

If we measure the efficiency of a code by how fast the code runs then we might not actually know if the code is efficient. This is because how fast the code run depends on the type of computer, the type and number of applications currently running on the CPU, the choice of programming language, and many more.

So, we measure the efficiency of code by Big O time complexity.

Big O tells us how long will it take for our code to execute based on:

  • size of input
  • number of steps to execute

So to know how efficient our code is, we must check how the number of steps in a code changes with respect to the number of inputs.

We will talk about the following Big O notations:

  • Big O(n)
  • Big O(1)

Big O(n)

Consider the following code:

Here, the function chooseAvengers() takes 4 steps to complete the above code.

However, if we had the following elements in the array:

const avengers = [“Iron man”, “Black Panther”, “Spiderman”, “Thor”, “Hulk”, “Captain America”];

chooseAvengers() will take 6 steps to complete.

And if we had the following array :

const avengers = [“Iron man”];

chooseAvengers() would take 1 step to complete.

So, as the number of array elements (inputs) increases the number of steps in the chooseAvengers() function increases. And as the number of elements in the array decreases the number of steps in the chooseAvengers() also decreases.

Let’s see this in a graph.

If there is 1 element then it takes 1 step to complete the execution.

If there are 2 elements then it takes 2 steps.

If there are 400 elements it takes 400 steps.

If there are 1000 elements in the array, the number of steps to execute in the code is also 1000. This type of time complexity is called linear time complexity. Which is also known as O(n).

O(1)

Now, consider the following code:

const avengers = [“Iron man”, “Black Panther”, “Spiderman”, “Thor”, “Hulk”, “Captain America”];const chooseAvengers = () => {    console.log(avengers[0]);}chooseAvengers();

Here, the chooseAvengers() function will have only 1 step, no matter how big the size of the array is.

The number of steps in the function doesn’t increase or decrease according to the size of the array.

Let’s see it in a graph.

Even if there are 1000 elements in the array, the number of steps is constant. This type of time complexity is called constant time complexity. Which is also known as O(1).

Now consider the following code:

const avengers = [“Iron man”, “Black Panther”, “Spiderman”, “Thor”, “Hulk”, “Captain America”];const chooseAvengers = () => {    console.log(avengers[0]); // O(1)    console.log(avengers[0]); // O(1)}chooseAvengers();

Here, the chooseAvengers() function will have only 2 steps, no matter how big the size of the array is.

We can say the time complexity of the above code is O(1) + O(1) which is O(2).

However, if the number of steps to execute does not change according to the size of the input, we can simply say the time complexity is O(1).

As the above program will have only 2 steps even if the number of elements in the array changes, we can say the time complexity of the above code O(1).

Here are the resources I learned time complexity from:

--

--

Palistha
Palistha

Written by Palistha

Learning and Sharing Everyday

No responses yet