*Attempt* to take ownership of the things you developed on your own time.
IANAL and it may vary by jurisdiction and contract interpretation by the court in question, but they will typically succeed if you willingly signed over your rights to any personal IP when you became employed, and it can be proven the development was done during the time of employment. Some companies are pretty ridiculous about this. The first time I ran into this, funny enough, was in college in the mid-80's when I applied at Radio Shack in the local mall. I don't know if their policy is still the same, but during the interview the store manager asked me what I did for hobbies and such. I told him that I liked to code, had published a couple of articles in programming journals, and tinkered with electronics and EEPROM programming. He immediately told me that if they hired me, I would have to sign over all rights to anything I created or developed on my time. The job was just floor sales, it wasn't even a development gig! Needless to say, I passed.
As for going to another employer, in theory, so long as it is a different (enough) implementation of the same algorithm, intellectual property laws say it is good to go.
Well yes, which is what I basically said in my last paragraph. If you take source code with you, then it's not a substantially different implementation. And just rewritten from scratch is not necessarily enough -- most programmers who've written the same algorithm multiple times can rewrite it from scratch and each iteration will be nearly verbatim. If such code is ever subpoenaed in a legal action and determined to be substantially similar (assuming again we're talking about a significantly unique or complex algorithm, obviously not something simple and widely-known like a quicksort or a Fisher-Yates shuffle), then there could be a liability.