Though I am not a lawyer...
You are conflating several issues. So, let's try to tease them apart.
There is copyright which applies to any code of sufficient size (more than a few lines?) that is re-used in another context. Here you need to abide by the copyright license of the code, which could range from forbidden to allowed. Open Source generally allows use and often with contingencies that you must propagate the copyright, attribute, and perhaps even also make your work also open source. When you reuse code under copyright, as long as you are compliant with the license, you can reformat the code as you wish. You have to honor copyright or else it is not just unethical but illegal.
If the copyright doesn't suite your needs, you can contact the copyright holders and ask their permission for another license, which they might do for free or for pay. You would have to obtain such permission from all copyright holders for the material, not just the principle holder.
If you are copy/pasting you need to concern yourself with copyright. If you are rewriting an algorithm from scratch, then not so much unless you are simply retyping (remember, IANAL).
There is another category of rights called moral rights, which is different from copyright. Plagiarism, claiming a work done by other(s) to be originally authored by you, is a violation of these moral rights. It is definitely an ethical violation, but may also have legal consequences. While we often assign (license) copyrights, by contrast, moral rights are not easily transferred (if at all). It would be wrong for someone legally holding exclusive copyrights to assume they can claim original authorship (they don't get that from copyright).
Regarding using an algorithm published by someone else, I'd use the golden rule: if either you feel that you are using an algorithm of merit that you would like credit for if you developed it and someone else used it, or you suspect that's how original the author feels, then attribution makes sense. Once attributed, you can modify as you see fit, reformat, etc..
Finally, algorithms may be protected by patent, in various forms. Patented algorithms are usually wrapped up in legal device of calling the algorithm a machine that does something, because on the face of it, algorithms, math, formulas cannot be patented. Ideally, a patented algorithm would documented as such that within code, but it is not a requirement, so this is a problematic issue. You have to decide for yourself whether researching into patent status is merited. Note that patents are a concern even if you develop the algorithm from scratch yourself, because an existing patent you don't know about may already cover that.
Search Wikipedia for Plagiarism, Moral Rights, Copyright...