What Is Big O Notation? A Beginner's Explanation

Nadim AnwarNadim Anwar
3 min read

Big O notation is a mathematical concept used to describe an algorithm's performance or complexity. It shows how the runtime or space needs of an algorithm increase as the input size grow. If the pace of an algorithm is not known, it will be difficult to determine the program's efficiency. Big O notation helps describe how much time an algorithm will take to run as the size of the input grows.

What is Time Complexity?

A way of showing how the runtime of a function increases as the size of an input increases

What is Space Complexity?

Space complexity is a way of showing how the memory usage of a function increases as the size of the input increases.

Let's understand with an example

Imagine a scenario where you have a file on your hard drive and you need to send it to your friend you need to send the file as soon as possible

file to system

Send it via email, FTP, or another electronic transfer method.

File SizePhysical TransferElectronic Transfer
1 MBTake Time: Time remains constant, independent of file sizeQuickly: The time taken to transfer a file increases with the size of the file.
1 TB or LargeIt can be done in a dayThis size of file can take more than one day to transfer

Performance variation based on size

Performance variation of an algorithm based on input

Let's understand with an example.

Suppose we want to buy a brand-new car. Obviously, we want to know more about the car's performance, which means we are interested in knowing how many liters of petrol it takes to drive 100 miles.

In the case of a car, there is no standard answer. Even though the manual mentions that it takes 70 liters of petrol for 100 miles, this information may not be correct since a car performs differently based on the conditions.

  • City Traffic - 20 L

  • Highway - 10 L

  • Mixed Condition - 15 L

So, here we see that the same car performs differently based on the conditions in which we drive it.

Similarly, based on the conditions given, an algorithm performs differently.

There are three scenarios for measuring any given algorithm:

  • Best Case

  • Average Case

  • Worst Case

Three Greek letters are used to define the cases of an algorithm.

Omega (Ω), Theta (Θ), and Omicron (O) also known as Big O

Let's consider an example

We have an array containing a list of numbers, and we need to find a specific number.

Array of 8 elements

  1. Find 1 - It is at the beginning of the array.

  2. Find 8 - It is at the end of the array.

  3. Find 4 - It is at the 4th position in the array.

So, finding 1 is quick and easy, but to find 8, we need to go through the entire array and check each element until we reach the end.

  1. Find 1 - Ω time complexity Best Case

  2. Find 4 - Θ time complexity Average Case

  3. Find 8 - O time complexity Worst Case

Runtime complexity of an algorithm

ComplexityNameSample
O(1)ConstantA simple add number function
O(N)LinearLoop through numbers from 1 to n
O(log N)LogarithmicFind an element in a sorted array
O(N²)QuadraticNested Loops
  • O(1) - Order of 1

  • O(N) - Order of N

  • O(log N) - Order of log N

  • O(N²) - Order of N²

0
Subscribe to my newsletter

Read articles from Nadim Anwar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nadim Anwar
Nadim Anwar

As an experienced front-end developer, I have expertise in building scalable and responsive web applications using modern technologies like ReactJS, Next.js, and TailwindCSS. With a solid understanding of JavaScript and a strong design sense, I excel at turning concepts into visually appealing and practical user interfaces. I am committed to continuously enhancing my skills and staying abreast of the latest industry trends to provide cutting-edge solutions.