r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

188

u/famrob Apr 05 '22

Big O. I’m quite far in the CS program at my school but the professor who tried to teach me big O was just absolutely horrible at it, so it went in one ear and out the other

166

u/tzaeru Apr 05 '22 edited Apr 05 '22

Big O basically tells how much time or space a function needs when its input grows in size.

For example, consider this algorithm:

function(int n):
    counter = 0
    for (i = 0; i < n; i++)
         for (j = 0; j < n; j++)
             counter++
    return counter

If n is 1, what's the number returned? It's 1*1 = 1. There was 1 increment operation done.

If n is 4, what's the number returned? It's 4*4 = 16. There were 16 increment operations done.

So you see the algorithm's time complexity grows by n^ 2. Big O notation is a way of expressing this; we can say that the time complexity is O(n2 ).

You can simply get the time complexity by counting the number of operations in the function and comparing how that number changes depending on the size of the input.

O(log n) is one very common time complexity and one example of an algorithm with O(log n) time complexity is searching a sorted array with binary search. The reason it's log n is that in each step, binary search splits the search space in two; so with input with size of 8 there are at most 4->2->1 or 3 steps, while with input size of 16 there's at most 8->4->2->1 or 4 steps.

4/3 = 1.33.. and log(16)/log(8) = 1.33..

94

u/Rote515 Apr 05 '22

Although this comment is true, i don’t think it gets to the heart of BigO, the most important part of bigO is that it’s a time complexity estimation more than an exact time complexity which your examples do not show. For example a 3N + 2N + 2N2 algorithm has a bigO of N2 as we drop the constants and only use the fastest growing term, because as N gets large it ends up being the only term we care about from a complexity point of view.

24

u/XilamBalam Apr 05 '22

Yes, it's looking how the function "behaves at infinity" (asymptotic behaviour).

→ More replies (2)

5

u/MaxwellCE Apr 06 '22

What if the algorithm is N² + N³? Could we say that has a big O of N³, or is the N² component non-negligible?

10

u/Rote515 Apr 06 '22

BigO only ever cares about the fastest growing term, so n2 + n3 would be N3 significance doesn’t matter, consider n = 20 in the above algorithm, n2 = 400 n3 = 8000 at that point n is negligible already, now imagine n=100 or n=1000 a larger power always makes the smaller power insignificant at any non trivial n

→ More replies (2)
→ More replies (2)

57

u/Rote515 Apr 05 '22 edited Apr 05 '22

BigO is super straightforward if you have a decent understanding of algebra. The general gist of it is that in computer science we only really care about the fastest growing term for time complexity, so for example if I have a function that is solved in the following number of steps. N+N2 where N is the size of the input, the the BigO time complexity is N2 since that’s the fastest growing term. The reason we care only about the fastest growing term is because when N gets very large that’s the only term that will really matter. For instance in the above equation if our input N is a list of 10,000 integers, well 10,000 operations is trivial for a modern processor, but 10,000*10,000 is not, that can take actual time to complete.

There’s a bit more to it than that, but that is the basics of BigO

Edit: one last thing, BigO is very useful but it doesn’t tell the whole story of time complexity, it’s a good way to understand the basics as N gets very large, but it can be misleading in certain algorithms. For example the equation 10000000n + n2 for the number of steps to complete an algorithm is still O(n2) time complexity, but because the first term has a super large constant it’s till likely not the most efficient solution depending on what you’re solving, there might be a case where an algorithmic n + n3 is better if n never gets very large for example even thought it’s O(n3) which is generally a very bad time complexity.

6

u/a_Delorean Apr 05 '22

Holy shit you explained this very well; thank you

→ More replies (5)

11

u/AlSweigart Author: ATBS Apr 05 '22

I always recommend Ned Batchelder's 2018 PyCon talk How Code Slows as Data Grows to explain big O in technical-but-approachable terms in under 30 minutes.

6

u/Marvsdd01 Apr 05 '22 edited Apr 05 '22

I'm not that good on explaining things, and I assume you're not asking "how to calculate an algorithm's memory or time complexity" - as Big O is directly related to that. So, in that case... When you ask "what is the Big O of an algorithm?" you are asking "how does my algorithm perform in the worst scenario possible?" If you think about a sorting algorithm, you can put the "worst case scenario" as the case where you try to sort a very large amount of numbers, for example. In this case, if this sorting algorithm would, for the sake of the explanation, have a Big O notation of O(n*log(n)) in terms of time spent running it, you're saying that the algorithm makes that amount of operations to complete the task of sorting an array. The "n" in the expression is a general representation of the size of the array, in this case. In most cases, you don't need an exact calculation of the algorithm's complexity, so we use an approximation.

Edit: spelling errors

5

u/Marvsdd01 Apr 05 '22 edited Apr 05 '22

Just so I can add a little more... Let's say you have two sorting algorithms: one performs at O(n2) in the worst case scenario, and other performs at O(n * log(n)). If you explode the size of the array you're sorting, then the difference between those two algorithms become very clear in terms of time spent sorting/number of operations needed to sort the array (as, mathematically, n2 is bigger than n * log(n)). That's the basic idea of using Big O notation (or any complexity notation, actually). If we want to check how two algorithms perform in terms of operations done or memory spent, it would be impractical (or sometimes impossible) to run these algorithms simultaneously and/or in the same running conditions to be able to compare them. Then we use a mathematical expression (complexity notation) so we can generalize performance of code in a way that we don't depend on computers to actually run it, and still can tell how this code is better or worse than another one.

Edit: the text became italic when I used an *

→ More replies (4)
→ More replies (7)