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

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

2

u/[deleted] Apr 06 '22

Do you happen to have an example of when this could be applied in a real life situation?

5

u/Rote515 Apr 06 '22

Which part? Just bigO in general? Literally any algorithm you ever write for any purpose can have bigO applied to it, it’s just a way of monitoring the steps it takes your algorithm to complete. Why it’s useful? I’m at a bar at the moment so no time/desire to type this out on my phone :D but take for example 2 unsorted arrays of size N integers, how would you test to see if those arrays held the same values? As in how would you test that {5,4,3,2,1} == {1,2,3,4,5}? There’s two easy ways off the top of my semi-drunk head to do that, you could do it using 2 loops, checking each element of the first array against the second array. It’s incredibly simple to write and has virtually no setup work. But it’s super inefficient when N is very large, it’s an N2 algorithm. The other way is you could sort each array first then check it using a single loop to make sure all indexes of the array match, that way is NlogN + NlogN + N simplified as 2NLogN + N which in BigO is O(NlogN), there’s a ton of setup work and data manipulation that increases both space and time complexity, but as N gets very large the second algorithm will be much faster.

You can write that yourself and test if you want, and if you really want to see it and can’t figure it out I’ll write it either tomorrow or Thursday for you.

That is an example that’s similar in concept to many you’ll run into in the real world, I just wrote one that manipulated data to take a list of charges and place them into an object where the charges are grouped by each customer. The algorithm I fixed was N2 time and mine was NlogN made a huge difference in time when N started getting large.

0

u/[deleted] Apr 06 '22

Hm interesting, I get the purpose of the principle. Just seems like it’s based on getting initial bad data. A good SQL statement and maybe using a little bit of LINQ would eliminate the need for this concept.

1

u/Rote515 Apr 06 '22 edited Apr 06 '22

Lol this is a misunderstanding of what bigO is in the extreme, the algorithm is irrelevant, that aside not all data comes from a database. Even if it was from a database, data frequently needs to manipulated in memory in ways SQL is unable to do… SQL will never eliminate the need to sort and organize data that’s already in memory rather than on disk…

2

u/[deleted] Apr 06 '22

I still don't understand this and I work as a programmer. I can never visualise what becomes N or when N is to the power of 2, etc.

Hello imposter syndrome.