DEV Community

Aimua
Aimua

Posted on

Big O Notation: Demystified

Overview

Hello fellow programmers, on my journey to in-depth understanding of computer concepts big o notation has been at the forefront of the most misunderstood concepts.
Personally it took me time to grasp, so I'm giving out these knowledge to my fellow Nigerians and/or West Africans.
This article is written in pidgin english for easier articulation and assimililation.
Let's get into it.

You don build code before? You wan know how your code dey play? make I yarn you about dis Big O Notation wey go give you the OT. No worry, I go yarn you for simple language wey you go take understand.

For Nigeria, we sabi say "time na money." If you write code wey dey run fast, you fit save time and even money. Big O Notation na like shortcut wey fit help you know how your code go take perform as e dey grow. E go show you as your code go take dey play as you dey add more and more data to am.

Before we enter Big O Notation, make we use example wey you go fit relate with. Imagine say you wan find your name inside big book wey get many many pages. You fit open the book from page one, you go find your name for the first page. But if dem ask you, how many pages you check? You go talk say na one page.

If the book get 100 pages, you fit still find your name, but you go check 100 pages. If the book get 1000 pages, you go check 1000 pages. The more pages wey dey the book, the more pages you go check. E fit take you more time to find your name if the book too big.

Now, make we talk about Big O Notation. Na system wey dem dey use to measure how the performance of your code take be as the input data dey increase. E fit tell you how your code go take play as e dey handle plenty data.

We go use some example to explain dis Big O Notation. If your code dey run for O(1), e mean say no matter how big the input data be, your code go always run for the same time. Na like say the book get only one page, you fit find your name for any page sharp sharp. For O(1), your code na boss.

But if your code dey run for O(n), e mean say as the input data dey increase, your code go take longer time. Na like say the book get many pages, you go dey check plenty pages to find your name. The bigger the input, the longer your code go take run am.

Imagine say you get one loop for your code wey dey run inside another loop. E go look like this:

for(i=0; i<n; i++)
   for(j=0; j<n; j++)
      print("foo");
Enter fullscreen mode Exit fullscreen mode

The outer loop go run n times, and the inner loop go still run n times.

If n=5, how many times foo go print? Well, the outer loop go run 5 times. And every time the outer loop run, the inner loop go run 5 times. So, for the total, foo go print 5 * 5 = 25 times.

If n=6, the outer loop go run 6 times, and the inner loop go run 6 times for every time the outer loop run. So, the total go be 6 * 6 = 36.

So, as you see, no matter the value of n, foo go print n * n times, wey be n squared. Na just the number of times wey the outer loop run, multiplied by the number of times wey the inner loop run. Na why the Big O of this code na O(n^2). The Big O just dey tell you how the workload dey grow as n dey grow.

If I just get one loop like this:

for(i=0; i<n; i++)
   print("foo")
Enter fullscreen mode Exit fullscreen mode

Foo go always print n times, so this one na O(n).
E dey useful because e dey show me how much work the program go do as n dey increase. So, let's say n=10,000. The program wey get O(n) go only print 10,000 times. But the first program wey get O(n^2) go print 10,000 * 10,000 = 100,000,000 times! So, for this reason, Big O dey show you which program dey more efficient. E dey useful to know how much work your program go do as your data take dey increase.

We get other Big O Notation like O(log n), and O(n log n), but make we no make our head scatter. Just know say as the "n" dey change, your code go dey behave different. You fit dey think say Big O na for big big code, but even small small code fit get Big O wey no too good.

So, how you go take use dis Big O Notation for your code? First, make you understand wetin your code dey do. Know how the input data dey affect the time wey your code go take run. If you sabi the Big O Notation of your code, you fit optimize am well well.

If you dey write code, Big O Notation fit be your padi. E go help you save time and resources. No waste time dey check all the pages for the book, focus on the important things. As you dey grow for your coding journey, make you dey think about how your code dey take waka.
As always, It's your boy kimonic. Peace and Blessings!

Top comments (0)