Best Book on Algorithms

I've learnt about C++ algorithms before, but until now I still can't use most of them properly in my coding, and there are some topics that I just can't comprehend, such as recursion.

I've read Introduction to Algorithms, but it hasn't helped me at all. Is there a book that can help me?
I've read Introduction to Algorithms

Which one? The famous one by CRLS CLRS?

If you don't understand recursion you'll have a hard time comprehending most data structures.
Any tree or graph, for instance, is a recursive structure by definition.
Last edited on
Yes, the CRLS one.
before buying an expensive book, use the internet a bit. See if you can get your head around recursion.

Most recursion is used to combine a loop with 'exploiting' the call stack as a 'free' data structure combined with a "base case" that can be solved without recursion.

Consider the silly factorial example. The base case is that fact(1) = 1.
The recursive definition of the function is that fact(n) = n*fact(n-1) unless n is 1, if n is 1 the answer is fact(1) = 1.

from there the core code will be

int fact(int n)
{
if(n == 1)
return 1;
return n*fact(n-1);
}

so for 3, it will compute 3*(fact(2)
for 2 it will compute 2 * fact (1)
for 1 it will compute 1 and return.
the return will multiply back in (continues where it left off in the previous call) and determine that 2*1 = 2. The 2 will return back and 3*2 = 6, and its all done. The intermediate results would be pushed onto the program call stack (but here, the compiler may just turn it into a loop for performance reasons). But conceptually they would be.

Start with that. See if you can understand it, code it, and get it working.
My professor at a top 5 computer science program in Canada recommended Algorithmics.

https://www.amazon.ca/Algorithmics-Spirit-Computing-David-Harel/dp/0321117840
Last edited on
... learnt about C++ algorithms before, but until now I still can't use most of them properly in my coding ...

if its the C++ algorithms from the standard library that you're referring to then try 'The C++ Standard Library (2nd ed)' by N Josuttis, specifically Chapter 11 'STL Algorithms'
Link to his website: http://cppstdlib.com/
Last edited on
Thanks for all the suggestions! However, I'm currently searching for a book that teaches algorithms that appear in competitive programming in a foolproof way. Is there one like this?

@jonnin I can understand the factorial example and code it, but beyond that? No.
closed account (48T7M4Gy)
FWIW
algorithms that appear in competitive programming

https://cpbook.net/
@kemort Thanks! That seems like a nice book. I'll definitely check it out.
Last edited on
Hi,

Also consider reading up about Design Patterns. They show ways of making code more reusable, decoupling etc.

https://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns

Note the patterns can be combined to make new patterns, here is an example:

http://www.marco.panizza.name/dispenseTM/slides/exerc/eventNotifier/eventNotifier.html

There also exists the concept of anti-patterns or "what not to do":

https://en.wikipedia.org/wiki/Anti-pattern

Good Luck !!
closed account (48T7M4Gy)
@hychan
This website is referred to at the cpbook site and might be useful.

https://visualgo.net/en
Topic archived. No new replies allowed.