Author: cyrus2281

Big O

How well a problem is solved

What is good code?

  1. Readable
  2. Scalable

time doesn't mean anything

How does the efficiency of the function changes as the number of the elements increases?

Calculating Big O

Rules

  1. Worst Cast
  2. Remove Constants
  3. Different terms for inputs
  4. Drop Non Dominants

While calclating the big O, we always consider to worst case scenario. For example if we are searching in an array, we should assume that the element is the last element in the array

Constant values that don't have meaning should be remove. For example constant value of substaction, addition, multiplication, and division don't have meanings and can be removed. That means O(100+5n-n/2) can be simplified as O(n)

2 seperate inputs should have different notations. for example a function with inputs of 'a' and 'b' has a big O of O(a+b)

if there are multiple O values, we should only keep the one with the heighest dominance. For example in big o of n+n^2, the big o is n^2

Iterating through half a collection is still O(n)

Things to count

Operations (+, -, *, /)
Comparisons (<, >, ==)
Looping (for, while)
Outside Function call (function())

bigocheatsheet.com



Big O notations

Linear

For loops, while loops through n items

Constant Time

No loops

Quadratic Time

Every element in a collection needs to be compared to ever other element. Two nested loops

Factorial Time

you are adding a loop for every element

Logarithmic Time

usually searching algorithms have log n if they are sorted (Binary Search)

Exponential Time

recursive algorithms that solves a problem of size N

Log Linear Time

usually sorting operations

Space Complexity vs Time Complexity

time complexity is the amount of time needed for the function to execute
space complexity is the amount of memeory needed for the function to execute

What causes space complexity?

Variables
Data Structures
Function Call
Allocations




References

Instructors

Aditional Resources