DEV Community

Cover image for Big-O Notation: Beginners Guide

Big-O Notation: Beginners Guide

Carlos Fuentes on November 12, 2018

In the world of programming write code as today is your last day on earth is not the main thing in the art of programming. Beyond understand what's...
Collapse
 
itachiuchiha profile image
Itachi Uchiha

I love this post. Very useful. Thanks Carlos!

Collapse
 
metcoder profile image
Carlos Fuentes • Edited

Thanks for your words. I love to help! Greetings :)!

Collapse
 
codedotjs profile image
Rishi Giri

Wonderfully written!

One suggestion - Next time when you describe something which involves writing code codes, don't use the screenshots, written code is more useful than the screenshots.

Thank You!

Collapse
 
metcoder profile image
Carlos Fuentes

True, sorry about that. Thanks for your suggestion, I'll do it in the next article! 😁

Collapse
 
mykalcodes profile image
Mykal

Awesome Post! Really helped my general understanding of Big O. that said, I did have a question regarding the O(n2) definition.

If you have a function that has 3 nested for loops, as opposed to 2 (as you have in your example) does the complexity then become O(n3)? I can't seem to see anything online that would say so...

Collapse
 
metcoder profile image
Carlos Fuentes

Hi! Thanks, I really appreciate it!

Yes, absolutely. If you make your for-loops grows with the same input, you can have something like an array of 4 spots and go through him three, four or five times growing the complexity in something like O(4 3, 4, 5 ...n ).

If you're going in a recursive way, the complexity it doesn't is O(n n ) and becomes exponential.

Collapse
 
mykalcodes profile image
Mykal

awesome!
That makes a tonne of sense! Thanks for the reply 😊

Thread Thread
 
metcoder profile image
Carlos Fuentes

You're welcome. I'm glad to helped you 🙏

Collapse
 
benalidjamel profile image
benali djamel

awesome !

Collapse
 
josephgalindo profile image
Joseph Galindo

Good post :)

Would the exponential example not be 100, 200, (...400), 800 though?

Collapse
 
metcoder profile image
Carlos Fuentes

Yes, sure!

Sorry, my bad. I'll fix it, thanks! ;)

Collapse
 
josephgalindo profile image
Joseph Galindo • Edited

No problem...did want to post back though since I saw the article was updated.

In the example you laid out, I think an algorithm running in O(2^n) time would be more like this:
10 items - 100s
11 items - 200s
(12 items - not listed - 400s)
13 items - 800s

Just wanted to point it out, since the typo makes the algorithm sound more performant than it really is.

Collapse
 
devhammed profile image
Hammed Oyedele

Thanks, this post really helped me alot!

Collapse
 
metcoder profile image
Carlos Fuentes

You're welcome. It's good to help!

Collapse
 
kiecodes profile image
kiecodes

Very well put!

Collapse
 
noor_codes profile image
Noorullah Ahmadzai

Thanks a lot Carlos!
Fantastic Topic

Collapse
 
mark_nicol profile image
Mark Nicol

What a super clear description.

Collapse
 
metcoder profile image
Carlos Fuentes

Thank's. I appreciate :)

Collapse
 
devhammed profile image
Hammed Oyedele

Thanks, this post really helped me a lot!

Collapse
 
mehdi89 profile image
Mehedi Hasan

A good read. Thank you.

Collapse
 
talitaoliveira profile image
Talita Oliveira

Thank you for this post!

Collapse
 
metcoder profile image
Carlos Fuentes

You're welcome. Is good to know I'm helping others!

Collapse
 
kwamikudjie profile image
Kwami Kudjie

Great post

Collapse
 
metcoder profile image
Carlos Fuentes

Thank you. I appreciate! :)