Skip to main content

Command Palette

Search for a command to run...

Big O Notation.

Published
•4 min read•View as Markdown
I

A Software Developer

Big O notation is like a special code that helps us understand how fast or slow a computer program can be. Imagine you have a big bag of toys, and your job is to find a specific toy inside it.

Let's say you search through the bag one toy at a time until you find the one you're looking for. If the bag has 10 toys, you might find it very quickly. But if the bag has 100 toys, it will take longer. The time it takes to find the toy depends on how many toys are in the bag.

Now, let's talk about Big O notation in terms of how many toys are in the bag (which we'll call "n") and how long it takes to find the toy.

1. O(1) - Constant time complexity: This means no matter how many toys are in the bag, it will always take the same amount of time to find the toy. For example, if you know exactly where the toy is, you can find it in just one look, whether the bag has 10 toys or 100 toys.

2. O(n) - Linear time complexity: This means that the time it takes to find the toy increases in a straight line as the number of toys in the bag increases. If there are 10 toys, it might take you 10 looks to find the toy. But if there are 100 toys, it might take you 100 looks.

3. O(n^2) - Quadratic time complexity: This means that the time it takes to find the toy increases much faster than the number of toys in the bag. If there are 10 toys, it might take you 100 looks to find the toy. And if there are 100 toys, it might take you 10,000 looks!

4. O(log n) - Logarithmic time complexity: Imagine the toys in the bag are arranged in a special order, such as from smallest to largest. If you're looking for a specific toy, you can use a smart strategy called "divide and conquer." You can split the bag in half and quickly decide which half the toy might be in. Then you repeat this process, dividing the remaining half until you find the toy. This approach is very efficient because you can eliminate a big portion of toys with each step. So, even if the bag has 100 toys, it might only take about 7 looks to find the toy. This is much faster than looking at each toy one by one.

5. O(n log n) - Linearithmic time complexity: This time, imagine you have many smaller bags, each containing a different set of toys. You need to find the toy in each bag and sort the bags in a specific order. For each bag, you can use the divide and conquer strategy we mentioned earlier (logarithmic time complexity) to find the toy quickly. However, you still need to go through each bag once (linear time complexity). In total, the time it takes will be the product of the number of bags (n) and the time it takes to search each bag (log n).

6. O(2^n) - Exponential time complexity: Now, imagine you have a magic machine that can create an exact copy of the toy bag for each toy inside it. For every additional toy you put in the bag, the number of copies doubles. So, if you start with 1 toy, you have 2 copies. If you have 2 toys, you have 4 copies. As the number of toys increases, the number of copies grows really fast. Now, your job is to look inside each copy and find the toy. This process takes a lot of time because the number of copies doubles with each additional toy. So, if you have 10 toys, you'll have 1,024 copies to search through. This exponential growth in the number of copies makes the algorithm slower as the input size increases.

Big O notation helps us understand how long a computer program will take to solve a problem. By knowing the Big O notation of an algorithm, we can estimate how efficient or slow it will be for different sizes of problems.

For example, let's say you have two programs to sort a list of numbers. One program has a Big O notation of O(n), and the other has O(n^2). If you have a small list of 10 numbers, both programs might be fast enough. But if you have a really big list with 1,000 numbers, the program with O(n^2) will be much slower.

So, Big O notation helps us compare different programs and choose the best one for the job. It's like having a secret language to talk about how fast or slow our programs are.

Thanks for reading.

D

Awesomely Crafted