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

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…