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

Show parent comments

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 *

1

u/SlowbeardiusOfBeard Apr 05 '22

One thing that I've never seen an explanation of is, how do you determine what the Big O of a given algorithm is in the first place. So, if I write a piece of code how can I look at it and think "hang on, this isn't very optimal, I should rewrite this by [doing some stuff] and now it's log n".

I've never known whether it's something that requires algebraic analysis, or if it should just be evident.

5

u/MrSloppyPants Apr 05 '22 edited Apr 05 '22

A very simple way is to look at how many times you loop within the algo. If you take in an array and pick out the first element, that operation is O(1) because it doesn't matter how big the array is, you are always executing the same thing. If you take the array and loop over it once, your algo is O(n) where n is the number of elements in the array. if you loop over the array twice, your algo is O(2n). If you loop through the elements in the array and for each element you loop through the entire array again, your algo is O(n2) because you are looping through the array for each element in the array. If you are only looping through some of the array, say, as part of a binary search, then the algo is O(log n) which is faster than O(n) because you are not traversing the entire contents of the array.

This is way over-simplified but it's a good starting point

→ More replies (2)