[Book Review] Mastering Algorithms with C
“Mastering Algorithms with C” is an excellent choice for learning many algorithms for C programmers.
It covers many basic and slightly advanced algorithms along with their full generic C implementations. You can see some nuances of the C language covered implicitly like generic arrays where you need the size of a single element to do pointer-arithmetic correctly.
If you need to learn more advanced algorithms, another book may be more suited to this. This book also has some issues with not including space or time-complexity correctly, and some of the implementations have some classic bugs. Space-complexity is rarely mentioned, if ever. So this is more of a C than an algorithms book.
An algorithm book that is more accurate and covers more advanced topics is “Algorithms (4th Edition).”
Strengths & Weaknesses
Strengths
- Proper use of double pointers
- A good illustration of generic arrays
- Effective use of both `memcpy` and `memset` to simplify the implementation
- Implementing simple data-structures and using them for more complex algorithms
- Excellent, consistent structure for each chapter
- Questions & answers that cover what is hard to cover otherwise
- Summary of related topics not explained or only explained briefly
- APIs for algorithms and data-structures make memory-management clear and shift the responsibility to the caller, which can help when only using static-memory, for example
- Extensive use of generic implementations even where most books with languages that have better generics support don’t
- Including linear-time sorting
Weaknesses
- A classic bug in merge-sort where memory is unnecessarily multiple times
- Space-complexity (let alone stack) almost never mentioned
- `realloc` used freely without counting toward complexity
- Complicated APIs due to gracefully handling cases like out-of-memory without terminating the program
- Little to no mention to concurrency or persistent data-structures
- Unnecessary nesting of code instead of just using guards
- Short and generic names make the code harder to read and follow
- Use of macros where functions suffice can lead beginners to do the same and learning the hard way the subtleties of macros
- The omission of time-complexity even with computation, for example with `cos`
- Reusing variables when introducing new ones would make the code much more readable (C90 may be one contributing factor)