Big O Notation – Explained as easily as possible


Subscribe to my e-newsletter and never leave out my upcoming articles

Data Constructions and Algorithms is about fixing considerations successfully. A sinful programmer solves their considerations inefficiently and a in fact sinful programmer would no longer even know why their resolution is inefficient. So, the ask is, How attain you deplorable an algorithm’s efficiency?

The straightforward retort to that ask is the Mammoth O Notation. How does that work? Let me indicate!

Impart you wrote a feature which matches through every quantity in a list and adds it to a total_sum variable.

Screenshot 2021-01-16 at 2.02.19 PM.png

Whereas you accept as true with “addition” to be 1 operation then running this feature on a list of 10 numbers will cost 10 operations, running it on a list of 20 numbers charges 20 operations and in an analogous plot running it on a list of n numbers charges the size of list (n) operations.

Screen Recording 2021-01-16 at 2.01.09 PM.gif

Now let’s desire you wrote any other feature that can maybe maybe maybe return the first quantity in a list.

Screenshot 2021-01-16 at 2.09.39 PM.png

Now, no topic how enormous this list is, this feature would possibly maybe maybe maybe no longer ever cost greater than one operation. Reasonably, these two algorithms hold utterly different time complexity or relationship between articulate of input size and articulate of operations completed. We focus on these time complexities the usage of Mammoth O Notation.

Mammoth O Notation is a mathematical notation broken-correct down to classify algorithms in step with how their plod time or set requirements develop because the input size grows.

Relating to the complexities as n, frequent complexities (ranked from appropriate to sinful) are:

  • Fixed – O(1)
  • Logarithmic O(log n)
  • Linear – O(n)
  • n log n – O(n log n)
  • Quadratic – O(n²)
  • Exponential – O(2ⁿ)
  • Factorial – O(n!)

Screenshot 2021-01-16 at 2.23.08 PM.png

Our first algorithm runs in O(n), which plot its operations grew in a linear relationship with the input size – in this case, the amount of numbers in the list. Our second algorithm is no longer reckoning on the input size in any appreciate – so it runs in fixed time.
Let’s buy a hold a look at what number of operations a program has to scheme in feature with an input size of n=5 vs n=50.

n=5 n=50
O(1) 1 1
O(log n) 4 6
O(n) 5 50
O(n log n) 20 300
O(n²) 25 2500
O(2ⁿ) 32 1125899906842624
O(n!) 120 3.0414093e+64

It could maybe maybe maybe no longer topic when the input is limited, but this gap will get very dramatic because the input size increases.

Screen Recording 2021-01-16 at 2.33.46 PM.gif

If n had been 10000, a feature that runs in log(n) would most fine buy 14 operations and a feature that runs in n! would set your pc on fire!

For Mammoth O Notation, we drop constants so O(10.n) and O(n/10) are every same to O(n) since the graph is accrued linear.

Screenshot 2021-01-16 at 2.47.52 PM.png

Mammoth O Notation is furthermore broken-down for set complexity, which works the same plot – how necessary set an algorithm makes employ of as n grows or relationship between articulate of input size and articulate of set wanted.

So, yeah! This has been essentially the simplest skill explanation of the Mammoth O Notation from my facet and I hope you enjoyed studying this. Whereas you chanced on this recordsdata correct, fragment it alongside with your of us on utterly different social media platforms and factor in clicking that observe button up there, it in fact motivates me to jot down extra.

Screenshot 2021-01-17 at 10.17.42 AM.png

And Whereas you REALLY liked the article, factor in purchasing for me a espresso 😊. Whereas you hold gotten any recommendations or recommendations, in fact be at liberty to let me know that in the feedback.

Overjoyed Programming!

Twitter Follow GitHub followers

Read More

Recent Content