BIG O NOTATION

donut

How efficient is your algorithm. Based on run time (how many steps do you have to take) and space complexity (how much memory do you need).
(YouTube)

Big O

O(1) - CONSTANT

CONSTANT

The input size doesn't matter. Whether your input (N) is 1 or 1,000 values long, the algorithm still just takes one "step." When you're planning a birthday party, you bake one cake, no matter how many people show up. O(1) has the least complexity, which is great.

Ex: Looking up an array when you already know the key or index; it doesn't matter how long the array is. (YouTube)

O(log N) - LOGARITHMIC

LOGARITHMIC

Cut the number of steps each time in half. If N is doubled, you only have to do 1 more operation. If 1 person is coming to your birthday party, you only need to bake 1 cake; 2-3 people require baking 2 cakes; 4-7 people require 3 cakes; and so on.

Ex: "Binary Search" in an ordered array - look at the middle of the array, see if the value is higher or lower than what you need, then repeat the process now only focusing on the half you need. (YouTube)

O(N) - LINEAR

LINEAR

N is the length of your input. If your list has 10 items, you print 10 times. Each birthday party guest gets a cake.

Ex: A basic "For Loop": for each item, do a specific thing. (YouTube)

O(N log N) - QUASILINEAR

QUASILINEAR

For each time N grows, the time required grows linearly and logarithmically.

Ex: A basic "While For Loop": While something is true, for each item, do this specific thing. Also, Quick Sort and Merge Sort. (YouTube)

O(N^2) - QUADRATIC

QUADRATIC

For each time N grows, the processing time doubles. If your list has 10 items, you have to print 100 times, or 10^2 times. If you bake a cake for each guest and you also write their names on each cake, 1 cake requires 2 steps (baking it and then writing a guest's name). 2 cakes require 4 steps, and so on.

Ex: Nesting two loops in the same function. Also "Selection Sort" and "Bubble Sort,"" when we have to iterate over each item and then compare to its unsorted items. (YouTube)

O(2^N) - EXPONENTIAL

EXPONENTIAL

Doubles with every additional input. If N = 2, your algorithm runs 4 times, or 2^2. If N = 3, your algorithm runs 8 times, or 2^3.

Ex: "Recursion" and "Fibonacci." (YouTube)

O(N!) - FACTORIAL

FACTORIAL

When you have to calculate every possible solution to determine the correct output. 6! = 6*5*4*3*2*1 = 720.

Ex: Brute Force and the Traveling Salesman problem. (YouTube)