Your code will be identifiable
Your coding style is going to be a little bit unique, distinct and recognizable no matter what you're programming, whether you're doing it for your employer now, or for yourself or a different employer later. So don't kid yourself that you can switch up a few variable names and make it look like different code. But that probably isn't the issue.
IP Ownership
If your employer pulled a description of the algorithm off the Internet, then your employer obviously doesn't own any IP in the idea, but they still own your work product if it is work for hire. Your work product is the digital copy of the specific code you produced for them, or printouts of the same.
They probably can't prohibit you from writing your own independent implementation of an algorithm where the original IP doesn't even belong to them. In fact, are you 100% certain your employer had a right to the IP themselves? ;-)
Work For Hire
If you are a W-2 employee you are by definition producing "work for hire" in which you have relinquished rights and ownership of your work product to your employer. I don't know who could guarantee that a good IP attorney couldn't make the case that a re-implementation from memory is materially the same thing as the original tangible work product.
If you're an independent contractor (1099), then this is going to depend on your contract. If your contract says it's work for hire, your employer owns the work product. If not, then your employer owns a perpetual license to use the work you produced, but you retain the copyright yourself, in which case you can do whatever suits you including selling the code you produced either on its own or as a part of your own product.
CAVEAT: I am not an IP attorney. But the law isn't all that obtuse on the subject.
Statutory Employee
Be aware that you could potentially be declared an employee retroactively, and your work declared to be work for hire if the employer controls your schedule, controls where you are able to do the work, controls the equipment you use, controls whether you can hire assistants or have to do all the work yourself, if the relationship is very long-term, if the employer pays you benefits that are normally considered to be regular full-time employee benefits, and so on.
Ethics
Is it such a generic algorithm that it can be used in totally different, non-competing applications? Or are you privately developing a product (of which the code in question is a component) which would compete with your employer's product? If so, quit your job with your employer now and eliminate your conflict of interest. Developing a competing product while working for your employer is ethically slippery, and might be legally actionable.
If your product is something unrelated, I probably wouldn't get worked up about it, unless you have some kind of restrictive employment agreement in place.
Employment Agreements
Did you sign a highly restrictive employment agreement, NDA, IP agreement or non-compete? That's going to vary by employer. In a job for an AT&T subsidiary once long ago I basically had to sign an agreement that said every stray thought I had any time of the day or night whether I was awake or dreaming, at the office, at home or on vacation, was their property as long as I was their employee and for something like six months after my employment with them ended, that if I developed a product even years after leaving them and they could prove that I had the idea for the product while in their employ that they would own that product and I would assist them in their legal effort to secure that ownership. There wasn't any provision that said the idea would only belong to them if it pertained to a line of business they were in at the time I had the idea. The agreement also said that I wouldn't take a job for anybody doing any of the same things they did for at least two years after termination of my employment.
The two-year non-compete was about keeping my knowledge, insights and experience obtained while working for them from being useful for anybody else and would have applied to working for myself, as well. Of course as a lifetime progresses, people acquire knowledge and insights from all kinds of sources that they take to their employer, rather than obtaining from their employer, especially in the Internet age. So I think proving the true genesis of any idea or technique can be genuinely trickier now than, say, 25 or 30 years ago.
In retrospect, I feel that kind of agreement is somewhat less than 100% ethical or fair, and I feel I was too young and dumb to realize they weren't paying me anywhere remotely close enough for a no-stakes wage-only position to deserve to have me sign an agreement like that. But it was what it was, I signed it, it was legal and I needed the work and experience and in the end I'm pretty sure my employer and I benefited each other mutually.
But the work for hire statutes apply regardless of the specific employment agreement(s) you sign.
Burning Bridges, Poking Bears
In general, intact bridges produce more opportunities than burned ones, and life is long and varied and you never know when you're going to encounter a potential opportunity with the same old employer, or with one of their friends or associates. If you go around poking bears, sooner or later one of 'em is going to bite you.
Do you think you might be able to approach your employer and just ask if they'd be okay with you rewriting this bit of code (not just copying what you wrote for them), for your own unrelated purposes, on your own time, at your own place, using your own equipment?