Hey student,
Big O notation is a complex concept to grasp, but ironically, the point of it is actually to try simplify the description of your code! The way I like to think about Big O notation is to think about the number of lines of code that have to be executed with extremely large inputs and to visualize what this actually looks like. This is probably better explained with some examples (look along with this image: https://apelbaum.files.wordpress.com/2011/10/yaacovapelbaumbigoplot_thumb.jpg):
def get_value(elements, index):
return elements[index]
This is O(1) (gray line) because no matter how big the elements
array is and no matter what the index
value is (assuming it's valid), the time to execute the code will always be the same.
def sum(elements):
sum = 0
for i in xrange(len(elements)):
sum += elements[i]
return sum
This is O(n) (blue line) because this loop will take longer and longer to run in direct relation to the size of the elements
array, i.e., the size of the data input.
O(N^2)
def sum(elements):
sum = 0
for i in xrange(len(elements)):
for j in xrange(len(elements)):
sum += elements[j]
return sum
First, don't worry about what this code is doing because it's a contrived example. This is O(N^2) because of the nested loop. Let's say the size of the elements
array were 1, this would only run 1 line of critical code. Let's say the size of the elements
array were 2, this would now have to run 4 lines of critical code (in combos of (i, j): (0, 0), (0, 1), (1, 0), (1, 1)). If the size of the elements
array were 3, this would have to run 9 lines of code. See how the number of lines of critical code that have to execute is the square of the number of input items? If you look at the graph, you can see that after a certain point, the execution time grows faster than the number of elements, whch means an O(N^2) might not perform well for large inputs.
O(log N) The typical example of an O(log N) algorithm (red line) is a tree traversal, which is a lot of code for now, but I can share with you later if you think it would be helpful. If you take a look at the graph, the important thing to see is that this is almost the opposite of an O(N^2) algorithm. Interestingly, it's slower than an O(N^2) algorithm for small inputs, but scales well for large inputs because the execution time doesn't grow as fast in relation to the number of inputs.
Hope that helps!
- Mentor