8 time complexity examples that every programmer should know

Adrian Mejia
11 min readApr 5, 2018

We are going to learn the top algorithm’s running time that every developer should be familiar with. Knowing these time complexities will help you to assess if your code will scale or not. Also, it’s handy to compare different solutions’ performance for the same problem. By the end, you would be able to eyeball different implementations and know which one will perform better.

In the previous post, we introduce the concept of Big O and time complexity. To recap:

  • The time complexity is not about timing with a clock how long the algorithm takes. Instead, how many operations are executed.
  • The number of instructions executed by a program is affected by the size of the input (usually referred to as n) and how their elements are arranged.
  • Big O notation is used to classify algorithms by their worst-case scenario. It a function of the input size `n`. E.g. O(n) or O(n²).

Here is a Big O cheatsheet and examples that we are going to cover on this post.

Big O examples covered in this post

Now, Let’s go one by one and provide code examples!

O(1) — Constant time

--

--