# Big-O Cheat Sheet

## Definition

Big-O notation, represents an algorithm's **worst-case complexity**. It uses algebraic terms to describe the complexity of an algorithm, allowing you to measure its efficiency and performance. Below you can find a chart that illustrates Big-O complexity:

Simply put, `O(1)`

stands for **constant time complexity**, which is the most efficient, while `O(n!)`

stands for **factorial time complexity**, which is the least efficient. The `n`

in the complexity represents the size of the input, so `O(n)`

means that the algorithm's time complexity will grow linearly with the size of the input.

Apart from Big-O notation, there are other notations that are used to describe the complexity of an algorithm, such as `Î©`

(Omega) and `Î˜`

(Theta). `Î©`

describes the **best-case complexity** of an algorithm, while `Î˜`

describes the **average-case complexity** of an algorithm.

## Common Data Structure operations

Different data structures have different time complexities for the same operations. For example, a linked list has `O(1)`

time complexity for `insert`

and `delete`

operations, while an array has `O(n)`

time complexity for the same operations. Below you can find average and worst time complexities for data structures used commonly in web development.

### Average time complexity

Data Structure | Access | Search | Insertion | Deletion |
---|---|---|---|---|

Array |
Î˜(1) | Î˜(n) | Î˜(n) | Î˜(n) |

Queue |
Î˜(n) | Î˜(n) | Î˜(1) | Î˜(1) |

Stack |
Î˜(n) | Î˜(n) | Î˜(1) | Î˜(1) |

Linked List |
Î˜(n) | Î˜(n) | Î˜(1) | Î˜(1) |

Doubly Linked List |
Î˜(n) | Î˜(n) | Î˜(1) | Î˜(1) |

Skip List |
Î˜(log n) | Î˜(log n) | Î˜(log n) | Î˜(log n) |

Hash Table |
N/A | Î˜(1) | Î˜(1) | Î˜(1) |

Binary Search Tree |
Î˜(log n) | Î˜(log n) | Î˜(log n) | Î˜(log n) |

### Worst time complexity

Data Structure | Access | Search | Insertion | Deletion |
---|---|---|---|---|

Array |
O(1) | O(n) | O(n) | O(n) |

Queue |
O(n) | O(n) | O(1) | O(1) |

Stack |
O(n) | O(n) | O(1) | O(1) |

Linked List |
O(n) | O(n) | O(1) | O(1) |

Doubly Linked List |
O(n) | O(n) | O(1) | O(1) |

Skip List |
O(n) | O(n) | O(n) | O(n) |

Hash Table |
N/A | O(n) | O(n) | O(n) |

Binary Search Tree |
O(n) | O(n) | O(n) | O(n) |

## Array sorting algorithms

Similar to data structures, different array sorting algorithms have different time complexities. Below you can find the best, average and worst time complexities for the most common array sorting algorithms.

Algorithm | Best | Average | Worst |
---|---|---|---|

Quick sort |
Î©(n log n) | Î˜(n log n) | O(n^2) |

Merge sort |
Î©(n log n) | Î˜(n log n) | O(n log n) |

Heap sort |
Î©(n log n) | Î˜(n log n) | O(n log n) |

Bubble sort |
Î©(n) | Î˜(n^2) | O(n^2) |

Insertion sort |
Î©(n) | Î˜(n^2) | O(n^2) |

Selection sort |
Î©(n^2) | Î˜(n^2) | O(n^2) |

Bucket sort |
Î©(n+k) | Î˜(n+k) | O(n^2) |