Big-O Notation Cheat Sheet: quick answers to Big-O questions
Tags: algorithms, mathematics, bigo, computerscience
Published: 2020-10-22 22:30:32

*This article was written by Jerry Ejonavi. Curious about Big-O Notation? Today, we put together a quick guide to answer all the commonly asked Big-O questions.*

___

**Big O notation** (sometimes called Big omega) is one of the most fundamental tools for programmers to analyze the time and space complexity of an algorithm. Big O notation is an asymptotic notation to measure the upper bound performance of an algorithm.

Your choice of algorithm and data structure matters when you write software with strict SLAs  or large programs. Big O Notation allows you to compare algorithm performance to find the best for your given situation. The Big O rating helps to ensure you have a lower running time than competitors.

This article aims to provide quick answers about common questions on Big O notation you might have or you might face in an interview. 

Let’s get started!



**Here’s what we’ll cover today:** 

* [What is Big O Notation?](#what)
* [What is space complexity and time complexity?](#complexity)
* [Big O Notation for data structures](#data-structures)
* [Big O Notation for sorting algorithms](#sorting-algorithms)
* [Wrapping up and next steps](#next)

<a name="what"></a>


## What is Big O Notation?
Big O Notation is a **mathematical function** used in computer science to describe an **algorithm’s complexity**. It is usually a measure of the runtime required for an algorithm’s execution.

But, instead of telling you how fast or slow an algorithm's runtime is, it tells you how an algorithm’s performance changes with the size of the input (size `n`). 

> The biggest factors that affect Big O are the number of steps and the number of iterations the program takes in repeating structures like the for loop. 


For our formal definition, we define {% katex %}O(g(n)){% endkatex %} as a set of functions and a function *f(n)* can be a member of this set if it satisfies the following conditions:

{% katex %}0 ≤ f(n) ≤ cg(n)0 ≤ f(n) ≤ cg(n){% endkatex %}

> constant `c` is a positive constant and the inequality holds after input size `n` crosses a positive threshold *n0*


If an algorithm performs a computation on each item in an array of size n, we can say the algorithm runs in {% katex %}O(n){% endkatex %}  (or it has constant time complexity) and performs the following work for each: {% katex %}O(1){% endkatex %} 

<br> 

<a name="complexity"></a>

## What is space complexity and time complexity? 
The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. Similarly, the Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input.


Both depend on several external factors such as computer hardware, operating system, etc, but none of these factors are considered when running an analysis on the algorithm.

Here's some common complexities you'll find for algorithms:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dl5oeq19sdlk3ajudlyi.png)



> **What are functions?** 
>
> A function is a routine that accepts an input and returns an output. It is expressed as below, where the output is defined in terms of the input `n`.
>
> {% katex %}f(n)=n^2{% endkatex %}

<br> 

<a name="data-structures"></a>

## Big O Notation for data structures

|  Data Structure | Insert  | Retrieve  |
| - | - | - |
| Array  | {% katex %}O(1){% endkatex %}  | {% katex %}O(1){% endkatex %}  |
| Linked List  | At Head: {% katex %}O(1){% endkatex %} <bR> At Tail: {% katex %}O(n){% endkatex %} | {% katex %}O(n){% endkatex %}  |
| Binary Search  | {% katex %}O(n){% endkatex %}  | {% katex %}O(n){% endkatex %}  |
| Dynamic Array  | {% katex %}O(1){% endkatex %}  | {% katex %}O(1){% endkatex %}  |
| Stack  | {% katex %}O(1){% endkatex %}  | {% katex %}O(1){% endkatex %}  |
| Hash Table  | {% katex %}O(n){% endkatex %}  | {% katex %}O(n){% endkatex %}  |

<br> 

<a name="sorting-algorithms"></a>

## Big O Notation for sorting algorithms

| Sorting Algorithm  | Worst-case scenario  | Average Case  | Best-case scenario  |
| - | - | - | - |
|  Bubble Sort  | {% katex %}O(n^2){% endkatex %}  | {% katex %}O(n^2){% endkatex %}  | {% katex %}O(n){% endkatex %}   |
|  Insertion Sort   | {% katex %}O(n^2){% endkatex %}  | {% katex %}O(n^2){% endkatex %}  |{% katex %}O(n){% endkatex %}    |
|  Selection Sort  | {% katex %}O(n^2){% endkatex %}  | {% katex %}O(n^2){% endkatex %}  | {% katex %}O(n^2){% endkatex %}  |
| Quick Sort  | {% katex %}O(n^2){% endkatex %}  | {% katex %}O(n log n){% endkatex %}  | {% katex %}O(n log n){% endkatex %}   |
|  Merge Sort  | {% katex %}O(n log n){% endkatex %}  | {% katex %}O(n log n){% endkatex %}  | {% katex %}O(n log n){% endkatex %}  |
|   Heap sort  | {% katex %}O(n log n){% endkatex %}  | {% katex %}O(n log n){% endkatex %}  | {% katex %}O(n log n){% endkatex %}  |

> **Note:** even though the worst-case quicksort performance is quadratic, but in practice, quicksort is often used for sorting since its average case is logarithmic.

<br> 

<a name="next"></a>

## Wrapping up and next steps
Now that you’ve gotten a handle on the common topics in Big O, you should be suitably prepared to tackle most questions you come across. There are other more technical topics that we didn’t cover such as,

* Complexity Theory
* Big Theta notation
* Lower Bounds Analysis
* Probabilistic Analysis
* Amortized Analysis
* Recursion

If you’d like to learn these topics, check out Educative’s course, **[Big-O Notation For Coding Interviews and Beyond](https://www.educative.io/courses/big-o-notation-for-interviews-and-beyond)**. The course explains the concepts in layman's terms and teaches how to reason about the complexity of algorithms without requiring an extensive mathematical skillset. 

*Happy Learning!*

<br>

### Continue reading about algorithm optimization

* [The insider's guide to algorithm interview questions](https://www.educative.io/blog/algorithms-an-interview-refresher)
* [Big O Notation: A primer for beginning devs](https://www.educative.io/blog/a-big-o-primer-for-beginning-devs)