In the US, a pure algorithm is explicitly exempted from being patentable as are mathematical facts and formulas and "ideas".
In reality, you can patent an algorithm (in the US). In fact, it doesn't even have to be in code and anyway when a patent is issued, it is NOT issued against source code, it's issued for "a series of steps performed" which of course is nothing more or less than an algorithm. Certainly working source code is not required, so the embodiment of the algorithm in code is not what's being patented. What's being patented is just the ethereal "idea" of something doing X then Y then Z in that order.
If this sounds like the system is talking out of both sides of it's mouth, then you've grasped what's going on... "No, of course you can't patent algorithms." "Sure, of course you can patent that algorithm."
Just to illustrate the fact that no code, working model, actual thing is associated with these patents consider that there are entirely successful businesses that operate as follows:
A entrepreneur sits around in a room with a bunch of "creative types", possibly some programmers, and some lawyers. They "brainstorm" about how some software product could be made to perform some useful function. They create nothing, no code, no prototype, nothing. At each step of the "product improvement process" a lawyer listens in, and when he/she recognizes something patentable, they note it. At the end of the day, the lawyer starts the creation of (or hands off the creation of) a new patent.
This is not made up; there are companies that do exactly the above. Each of these patents is a algorithm, since it does nothing except specify a series of steps to be taken at various points in time.