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/Sasquatch_actual Apr 06 '22

I can't stand using books for stuff like this.

The most simple way to learn time complexity stuff is to code up some simple demonstrations.

The trick to this is using ridiculously large datasets.

If your dataset has 10 numbers in it, anything you do is going to complete nearly instantly after you click run.

Make an array with 500 million integers in it.

Try some constant time operations. They happen instantly.

Try some linear time operations. Search for 50k random integers between 0 and 500 million. That's going to take a while (minutes) to complete. Print out a timestamp before and after that process, see how long it takes.

Do the same thing again. But this time use binary search in logarithmic time. It'll blow your mind how fast it is. A few seconds maybe.

Play with each time complexity with large enough numbers to make a difference. Some may never finish,

Look at your data structures, and their different complexity for different operations. Think how loading half a billion items would be fast or slow.

Any operations that's making you iterate over that entire length of half a billion you'll feel it, when you try it.

1

u/CodedCoder Apr 06 '22

My issue is knowing how to do it, like no one has ever explained it to me of how to get the time that’s the part that gets me like the last book I read was basically like “so one loop is 0(n) “ not saying that’s true but using it as an example and I go but how is it that, how did you get to that conclusion lol

1

u/Sasquatch_actual Apr 06 '22

Sounds like you need more of a higher level conceptual understanding.

Using a list an example. 1,2,3,4,5

If your operation, requires you to possibly check every single number in that list. That's considered linear O(n). That basically means the longer the list is, the longer it takes you check every number in it. As the list gets bigger, there are more numbers for you to check. That's the linear relationship, add 20 numbers to the list, that's directly 20 more numbers you have to check.

Using the same list, but now logarithmic o(lg) instead of checking the entire list 1 and at time, you now check the halfway point and ask is my number larger or smaller, then cut your list in half and do it again.

The overall simplest way to more or less skate by is just look directly at how your traversing data set. If you can see your starting at the beginning and touching each number until the end then you're o(n).

If you're instantly referencing the one you want without looping through the whole set it's o(1).

If your doing something fancy to significantly shorten the amount you have to search through each time you iterate. Like cutting your list in half or thirds each time then it's likely logarithmic.

A loop inside of a loop you multiply the complexities together. Two loops not inside each other you just add them.

1

u/CodedCoder Apr 06 '22

You are awesome seriously, this I understand, why can't the books make it sound this simple, now I GET it way more. it makes sense, and I think I can actually see my code and what it would be. other people try to explain it so technical, sometimes someone just needs it laid out in layman's terms lol thank you for this, it has been bothering me a good bit because I want to get a ton better at what I do and this was just stumping me.

1

u/Sasquatch_actual Apr 06 '22

So try this. This question was on my dsa test a while back.

What would the loops look like if I asked you to print your name n2 * lg times?

Use 3 for loops.