Your second algorithm is linear, and in the end it will make one pass through your list. In this case, your first algorithm has an exponential runtime (which is the worst). You end up essentially checking everything in the list to determine that the first element, 1, is not the maximum. Then you look at the second element, 2, and look at the rest of the list to find out that it is also not maximal.
If you run your program with mx
, and values ββlike 22
, 23
, ..., 30
, you will see a clearly exponential increase in runtime.
In particular, this is not just a matter of tail recursion, and not, it is an inefficient recursive algorithm versus efficient. You can implement them in a language without tail recursion and still see mx'
better performance over mx
.
source share