It's a horrible algorithm, no offence. No need to do this recursively at all.

But the mistake is that you don't have a return statement for "alg" for every path of execution. That's all I'm going...