The Big O End! Part 3 of 3
By: Osha Groetz
Tags: Software Development, Coding, Software Engineering, Programming, Big O Notation

The Big O End! Part 3 of 3

Wrapping Up Our Last 3 Growth Notations

3d Wireframing in Adobe Illustrator — With Gradient on Editable Text

Big O Notation is such an immense topic to learn and understand. I read an article that said ‘you don’t have to be a math genius to understand it’. They were lying. Writing these articles, though, has helped me get a better grasp on the subject and just this past week while in an algorithm meetup, I had my first AH-HA moment when someone asked me it was possible to develop the algorithm with an O(1)…before I even knew what was happening…I spouted out “No, because we will always need to iterate over the entirety of the elements, the best we can even hope for is an O(n).” I genuinely looked around to see who had said that!

This article is going to cover the last 3 growth notations I set out to cover. If you’d like to check out the other 2 parts of this set of particles, I’ll link them at the end of this post. Message me if I have missed anything, misspoken, or flat out am wrong about any of my information — love learning and love being right!

O(n²)

This notation is know as Quadratic Time. This time complexity is used to denote squaring, or multiplying a number by itself. We have a quadratic (or polynomial) time complexity every time we nest multiple iterations, or have multiple for loops. I found an easy to understand, great example of this in a link on stack overflow, and changed their Python example to Javascript:

linear example:

for (const x of Array(10).keys()) { console.log(x)} quadratic example:

for (const x of Array(10).keys()) {

for (const y of Array(10).keys()) {

console.log(x, y)

}

}

If you open up your console, command-opt-j on mac, and copy in the linear example and return, you’ll see the log returned is only 10 elements long, printing 0–9. If you then copy in the quadratic example and return, you will receive 100 elements. Because of the 2nd for loop…we end up with a logged set that logs x=0, and then the 10 values of y, and continues on in this pattern x+1 while x < 10. The input to this function is 10 and we end up with 100 iterations, a quadratic growth notation where n(the number of elements) is squared. What if the input was 100? Our iterations would increase to 10,000 because O(100²)=10,000. The effort, or time needed to run the equation directly corresponds to the number of elements as the square of the number of elements.

O(2ⁿ)

A growth notation of O(2ⁿ) is referred to as Exponential Time. In this growth notation, the number of times it takes to complete the algorithm is directly 2 squared to the n’th power, with n, as always, being the number of elements. You could imagine, that this kind of growth is figuratively, out of control. and will quickly become an immense amount of every single element added to our data set.

Exponential growth happens when the algorithm must evaluate every permutation or combination in order to complete its task. A fast example of this: maybe you are hacking a password (don’t do this obviously!), worst case scenario, you have to try every possible combination of letters/numbers in order to get the correct combination. If you started with 10 elements, you’d have 1024 combinations to work through, if I then give ask you to figure out the password, but now it has 11 elements in it, you’d have 2048 combinations to try. This is exponential growth…every element requires the equation to double itself, again. This is the recursive Fibbonnaci algorithmic example with an exponential growth¹ (although it’s space complexity is O(n)):

fib(n){

if (n <= 1) {

return 1

}

return fib(n — 1) + fib(n — 2)

}

O(n!)

Factorial Time complexity. Factorial is if you have a number and you multiply it by all positive numbers less than it. For example²:

5! = 5x4x3x2x1 = 120 20! = 20x19x18x17x16x15x14x13x12x11x10x9x8x7x6x5x4x3x2x1 = 2,432,902,008,176,640,000

The most common example of an algorithm with a factorial time complexity is permutations of a string. If you have a string of ‘fan’ and you have to find all permutations, you will have an ouput of 6, but if you have the string ‘hello’ there will be an output of 120 possible solutions!

Thank you for reading my Big O Notation series! I can not believe how much I feel like a learned, but it’s such a miniscule amount of info from the Big O picture. Have a great week y’all!