Skip to content

Big O Notation

B ig O notation is a way of describing the efficiency of algorithms. It’s used to help programmers understand how much time and memory an algorithm will need to solve a problem.

Let’s start with a simple example. Suppose you have a list of numbers, and you want to find the maximum value in the list. One way to do this is to look at each number in the list, one at a time, and remember the largest number you’ve seen so far. This algorithm is called “linear search”, and it has a Big O notation of O(n), where n is the number of items in the list.

The “O” in Big O notation stands for “order of magnitude”. It tells us how quickly an algorithm’s time or memory requirements will grow as the size of the input increases. For example, if an algorithm has a Big O notation of O(n), it means that if you double the size of the input, the algorithm will take roughly twice as much time or memory to complete.

Here are some common Big O notations you might encounter:

  • O(1) means that an algorithm’s time or memory requirements don’t depend on the size of the input. For example, if you want to look up a value in a dictionary, you can do it in O(1) time, because the dictionary uses a hashing algorithm to quickly find the value you’re looking for.
  • O(log n) means that an algorithm’s time or memory requirements grow logarithmically as the size of the input increases. This is common in algorithms that use binary search or divide-and-conquer techniques.
  • O(n) means that an algorithm’s time or memory requirements grow linearly as the size of the input increases. This is common in algorithms that look at each item in a list or array, one at a time.
  • O(n log n) means that an algorithm’s time or memory requirements grow faster than linearly, but slower than quadratically, as the size of the input increases. This is common in algorithms that use sorting or merge techniques.
  • O(n^2) means that an algorithm’s time or memory requirements grow quadratically as the size of the input increases. This is common in algorithms that use nested loops to compare each item in a list or array to every other item.
  • O(2^n) means that an algorithm’s time or memory requirements grow exponentially as the size of the input increases. This is common in algorithms that use recursive techniques.

It’s important to note that Big O notation only tells us about the “worst case” time or memory requirements of an algorithm. It doesn’t tell us anything about the “average case” or “best case”. It’s also not a measure of actual time or memory usage, but rather a way of comparing algorithms in a theoretical sense.

Leave a Reply

Your email address will not be published. Required fields are marked *