Big-O Notation

For people that hate math

* Creator of bechdel.io

I am
Joe Karlsson

No, not that Big-O

Measures how an app will scale when you increase the amount of things it operates on

Why  should you care?

If my CS profs knew I was teaching y'all this

O(1)

constant time

No matter how large the input is, the time taken doesn’t change

const arr = [
  'accessing', 'an', 'array',
  'element', 'takes', 'constant', 'time'
];
const index = arr[4];
const isEven = (num) => {
  return num % 2;
}

Accessing any single element in an array

Determining if a number is even or odd

O(log n)

logarithmic time

Any algorithm which cuts the problem in half each time

// Function to recursively print the right half of a string
const splitRight = (str) => {
    let length = str.length;
    
    // Helper function
    const help = (index) => {
        
        // Recursive Case: Print right half
        if(index < length){
          
            // Prints characters from index
            // until the end of the array
            console.log(str.substring(index, length));
            
            // Recursive Call: call help on right half
            help(Math.ceil((length + index)/2));
        }
        
        // Base Case: Do Nothing
    }
    help(0);
}

O(n)

linear time

The larger the input, the longer it takes, in an even tradeoff.

 

Every time you double n, the operation will take twice as long.

const sumOfArr = (arr) => {
  var sum = 0;

  for (let i = 0; i < arr.length; i++){
    sum += arr[i];
  }
  return sum;
}

O(nLog(n))
O(nLog(n))O(nLog(n))

loglinear time

Performs an

O(log n) operation for each item in your input

Most (efficient) sort algorithms are an example of this.

def mergesort(list)
  return list if list.size <= 1
  mid = list.size / 2
  left  = list[0, mid]
  right = list[mid, list.size]
  merge(mergesort(left), mergesort(right))
end

def merge(left, right)
  sorted = []
  until left.empty? or right.empty?
    if left.first <= right.first
      sorted << left.shift
    else
      sorted << right.shift
    end
  end
  sorted.concat(left).concat(right)
end

O(   )

quadratic time

n^2
n2n^2

For every element, you do something with every other element, such as comparing them

const makeGrid = () => {

  const grid = document.createElement('table');
  grid.className = 'grid';

  for (let r = 0; r < this._rows; ++r) {
    let tr = grid.appendChild(document.createElement('tr'));

    for (let c = 0; c < this._cols; ++c) {

      let cell = tr.appendChild(document.createElement('td'));
      cell.id = "grid" + i++
    }
  }
}

O(   )

exponetial time

2^n
2n2^n

The time taken will double with each additional element in the input data set

let size = 5;

const fork = (i) => {
  if( i < size ){
    fork(i+1);
    fork(i+1);
  }
}
fork(0);

O(n!)

factorial time

O(n!) involves doing something for all possible permutations of the n elements.

Brute Force Password Crackers

Pop Quiz

def bubble_sort(list)
  for i in 0..(list.size - 1)
    for j in 0..(list.size - i - 2)
      if (list[j + 1] <=> list[j]) == -1
        list[j], list[j + 1] = list[j + 1], list[j]
      end
    end
  end
  return list
end
def factorial(n)
  if n.zero?
    return 1
  else
    return n * factorial(n - 1)
  end
end
def palindrome?(input)
  stack  = []
  output = ""
  input.each_char do |x|
    stack.push x
  end
  while not stack.empty?
    output << stack.pop
  end
  return (output == input)
end
const loop = (N) => {
  if(N < 0) {
    return N;
  }
  return loop(N-1) + loop(N-2) + loop(N-3);
}
let size = 5;
function fork(i){
  if(i <= size){
    for(let j = 0; j < i; j++){
      fork(i+1);
    }
  }
}
fork(1);

Resources

Feedback Welcome!

Please fill out this feedback form:
https://goo.gl/forms/Hsn6oonjyIl1yxCm1

Big-O

By Joe Karlsson

Big-O

For People That Hate Math

  • 2,444