Skip to content

Instantly share code, notes, and snippets.

@astout
Created October 7, 2018 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astout/6b5b8398700d545f21337c43c00eb71d to your computer and use it in GitHub Desktop.
Save astout/6b5b8398700d545f21337c43c00eb71d to your computer and use it in GitHub Desktop.
Thinkful Algorithms Response

Algorithms

Hi student,

What I found most helpful when I was learning Big-O notation was to always think of it like this. Simply, Big-O notation is

How execution time scales with respect to size N of the dataset.

Here are some basic examples of different notations.

O(1)

O(1) means "constant". No matter how big the dataset is, the time it takes the algorithm to return the result will always be the same.

O(N)

O(N) means "linear". The size of N has a linear effect on the time it takes the algorithm to return the result. This is most commonly seen in algorithms that require looping up to one full time through the dataset to find the result.

O(N2)

O(N2) means squared growth. This is common in algorithms loop through a dataset once and for each item in the dataset, it loops through the dataset again. These are commonly called "nested for loops".

O(2N)

O(2N) means "exponential". The size of N has an exponential effect on time. The bigger N is, time grows exponentially! This can be seen when looping with recursion. This means that for each item of a dataset, a function calls itself on a subset of the data.

O(log N)

O(log N) describes an algorithm that starts slightly better than linear for the size of N, but flattens toward constant the bigger n gets. The most common example is binary search.


When trying to identify the Big-O notation of your algorithm, you'll need to know how to compare your algorithm to the simple definitions above. Then simply come up with sample datasets and inputs for your algorithm. Try to think of what the best case scenario is for your algorithm, typically its something that you're looking for and it's the first you result you find. Now think of the worst case scenario. What's the hardest thing for your algorithm to find? Big O is the worst case scenario or the upper bound of your algorithm. Now just identify which of the examples above best illustrates your worst case scenario.

Hopefully this helps. Please let me know if you'd like to discuss it further or work through more examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment