When you take an algorithm from a book, or copy and paste from stack overflow, you put a comment in the code with a link to the source, really no different than how you'd cite a quote in a paper.
To me, it just doesn't seem like it's even remotely challenging to figure out how to do this, or when to do this. If it's not yours, say where you got it.
When in doubt, cite it. What exactly would the harm be if you cited something when you weren't sure if it was necessary, anyway?