Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People

by: Aditya Bhargava (0)


Grokking Algorithms is a fully illustrated, friendly guide that teaches you how to apply common algorithms to the practical problems you face every day as a programmer. You'll start with sorting and searching and, as you build up your skills in thinking algorithmically, you'll tackle more complex concerns such as data compression and artificial intelligence. Each carefully presented example includes helpful diagrams and fully annotated code samples in Python.

Learning about algorithms doesn't have to be boring! Get a sneak peek at the fun, illustrated, and friendly examples you'll find in Grokking Algorithms on Manning Publications' YouTube channel.

Continue your journey into the world of algorithms with
Algorithms in Motion, a practical, hands-on video course available exclusively at (​in-motion).

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the Technology

An algorithm is nothing more than a step-by-step procedure for solving a problem. The algorithms you'll use most often as a programmer have already been discovered, tested, and proven. If you want to understand them but refuse to slog through dense multipage proofs, this is the book for you. This fully illustrated and engaging guide makes it easy to learn how to use the most important algorithms effectively in your own programs.

About the Book

Grokking Algorithms is a friendly take on this core computer science topic. In it, you'll learn how to apply common algorithms to the practical programming problems you face every day. You'll start with tasks like sorting and searching. As you build up your skills, you'll tackle more complex problems like data compression and artificial intelligence. Each carefully presented example includes helpful diagrams and fully annotated code samples in Python. By the end of this book, you will have mastered widely applicable algorithms as well as how and when to use them.

What's Inside
  • Covers search, sort, and graph algorithms
  • Over 400 pictures with detailed walkthroughs
  • Performance trade-offs between algorithms
  • Python-based code samples

About the Reader

This easy-to-read, picture-heavy introduction is suitable for self-taught programmers, engineers, or anyone who wants to brush up on algorithms.

About the Author

Aditya Bhargava is a Software Engineer with a dual background in Computer Science and Fine Arts. He blogs on programming at

Table of Contents

  1. Introduction to algorithms
  2. Selection sort
  3. Recursion
  4. Quicksort
  5. Hash tables
  6. Breadth-first search
  7. Dijkstra's algorithm
  8. Greedy algorithms
  9. Dynamic programming
  10. K-nearest neighbors.

The Reviews

This was a good attempt at something marvelous, unfortunately it is only that: an attempt; the book falls rather short. I dearly wanted to give a very positive review for this book as the premise of it was incredibly promising. I purchased it, and it sat on my bookshelf several months before I opened it beyond a quick peruse of a few pages. Those brief glances at the opening pages only added to my excitement about sitting down with the book later.However, once I actually did get the time to read this book cover to cover, I was immensely disappointed. The three stars given are primarily because the book starts off very strong; the coverage of binary search and the intro to Big-O provides precisely what was promised. Yet, these are only about the first 20 pages. After this beginning, the book becomes somewhat erratic, I cannot for the life of me understand why more of the sorting algorithms were not covered. The only sorts covered are quicksort and selection sort. There is a section titled 'Mergesort vs Quicksort' in which merge sort is only mentioned, not described or explained at all, and this is only a single paragraph; insertion sort gets only a sentence.The book's treatment of recursion is fantastic, but gets at my worst frustration with this book: the author discusses and thoroughly describes basic recursion, providing excellent illustrations of the call stack during execution and tracing the entire execution of a recursive function. At the end of this exercise, he mentions tail recursion (a critical aspect of recursion that more than merited the explanation, which would not have taken much more considering he’d already explained the call stack) and says it's beyond the scope of the book. However, the second half of the book is then dedicated to advanced topics like K-nearest neighbors (solid explanation) and just mentioning things that one would end up studying elsewhere on one's own. A whole chapter is dedicated to Dijkstra's Algorithm (again, solid coverage), which is a somewhat advanced algorithm that benefits from his illustrations and descriptions, but leaves to question, who made the decision to leave out a more complete description of recursion and thorough coverage of searches and sorts in lieu of what at the end seems like filler chapters? Anyone who is at the point of needing to understand those two algorithms likely won't read an illustrated book on the topic; to me, these topics supplant those someone intrigued by the idea of this book might've come seeking.There are errors, but they're no biggie. The very first piece of code in the book contains an omission that causes it to malfunction (setting of the variable 'mid' should divide the sum of 'high' and 'low' by 2). I came to realize it may have been due to a page layout or typesetting issue as I found the same problem in several chapters. It seems the edges of the code are partially covered by images. The coverage of Big-O beyond the intro chapter is a little weak, and only helped because I had some previous knowledge of the concepts surrounding it, and the fact that much of the explanation is repeated several times, so some bits finally sink in. I could see how someone coming to this book hoping to have 'aha' moments about algorithms would be left very lost.The author also uses the technique of describing how something works in a simple way and then a section later taking it all back and saying 'this is really how it works'. I've seen this before, and it can be effective, but a writer must take care the manner in which he simplifies the initial example. This happens rather confusingly in the explanation of hash tables (associative arrays) in relation to their performance. If you're following his description of them, its clear that him saying they return values 'instantly' is somewhat odd particularly when you understand what O(1) actually means. When he follows up to clarify, which he does by describing how hash tables can be slow in some cases, the clarification isn't really satisfying as you didn't really absorb the chapter wondering if perhaps you missed something before since hash tables were glowingly described as the data structure to end all data structures. He then goes into discussing (incompletely) the implementation of hash tables, which he keeps saying 'you'll never have to do'. He then trails off with a brief description of things to consider when using them without really implementing a hash table in code as suggested (examples 'using' hash tables are given, but the mention of 'implementing' them comes after this and indicates an upcoming example which never materializes).I wrote this review only as a warning. The implementation of the book is a solid 2 stars considering the price; the first half gets it to 3. The book is about 50% as described, 50% the author seemingly trying to prove he has a CS degree. Perhaps friends told him the book was too simplistic. Unfortunate. The heavy appearance of 5 star reviews here greatly misled me. Reading them afterward, it seems most are praising the idea of the book and not its actual execution. Had I read it during the return period, I would have returned it. I am a intermediate-level programmer. I understood most of these topics at a basic level beforehand. I ordered this book because I had been recommending it to others, who wanted to learn about algorithms from scratch, as the premise of it sounded excellent. I finally decided to read it myself since I've been raving about it for the last year; I wish I had read it first. It's a fun read, but don't expect any epiphanies. If you're a beginner, you're actually better off searching "basic sorting/searching algorithms" online and learning from YouTube videos until you can grasp the content of a college textbook (or one of the programming interview prep books which cover algorithms very thoroughly without the math of a university text). This book starts off great, but finishes more like a set of blog posts written over the course of a year, by a person who couldn't wait to prove to you how smart he was becoming day by day. I know many times a project can start off as one thing, and then after input by many people and via your own desires to do something great, lose its way; it seems that may have happened here with the author deciding that the basic algorithms were too mundane, and choosing to try and explain more impressive concepts which somewhat ironically, are ‘beyond the scope of this book’.

I’m a front end dev without any formal computer science education. In the past, when I heard terms related to algorithms and big O notation, they seemed scary and made me feel totally unqualified to be part of that world. But the reality is, a lot of these concepts seem more difficult than they really are. When they’re presented in simple terms with examples and illustrations, you start to realize that it’s not beyond you at all - you just haven’t learned it yet. This book does an incredible job of making these concepts accessible. I recommend it to anyone without a formal CS background, whether you’re new to programming, or like me, have been doing it for a long time but haven’t had much exposure to algorithms. I recently used this book as a resource when preparing for coding interviews, and it was invaluable. I give it a ton of credit for helping me land my current job in a role I didn’t think was possible for me just a few years ago.

I'm gonna keep this short (EDIT: that..definitely didn't end up happening) because I haven't gone through the whole book yet, so **disclaimer** on that part. But after just the first couple of chapters, I was very impressed.I'm a 'seasoned' programmer, I would like to think (industry experience at a “top” company, CS degree, coding all my life, etc), and so almost all of this book is "review" for me. I'm going back through fundamentals in preparation for coding interviews, as I'm back on the market for a job. I'd say this book does these things very well:1) Fills in the gaps that might've always been there - What I mean by this is, if you maybe got a bachelors in computer science, there could easily have been some material that just didn't completely sink in or that your curriculum didn't focus on. I think going through this book is a great way to make sure those gaps are filled.2) Explains concepts in an easy to grasp form - The examples that are used in the book are great. There's one example early on with big O involving drawing 16 boxes on a piece of paper. One way is to draw 16 boxes one at a time - yielding O(n). Another way is to fold that piece of paper in half each time. This gets your 16 boxes in only 4 folds - big O(log n). It's simple, yet a great way of showing the difference in the two.3) Keeps your attention - I love to buy books and then not read them… it’s a talent that I exercise often… One thing I can say about this book is that it actually keeps my attention, and I enjoy reading it. That’s saying a lot if it can do that. How much good is a book if it’s too boring to focus on and get through? If you don’t read it, it doesn’t matter how quality the content is.I’ve recommended this book to several people in my life already, and I wish I still had my Amazon affiliate account setup because I feel like a freaking spokesperson for the thing..! Haha.In summary: I would recommend this book to a very wide range of people—ANYONE in computer science looking to get a job, anyone trying to get a degree or just take anything CS related, anyone interested in some of the CS fundamentals, anyone looking to review computer science concepts, anyone wanting to dip their feet into a new field of study they haven’t explored before.I wish I had this book when I was an undergrad—It would’ve saved me so much headache and difficulty.I’ll update this review once I’ve finished more of the book, but from what I’ve gone through so far, and compared to plenty of other programming books (algorithms, interview prep books, etc), it’s by far my favorite.

Aditya Bhargava does an amazing job explaining core concepts related to algorithms using pictures, diagrams, examples, and exercises sprinkled throughout his book.He uses Python for his code examples which is a very accessible programming language in terms of both popularity and readability. This is a great choice for beginners who will likely be checking this book out first before diving deeper.Finally, he points out the topics he considers to be outside the scope of the book. This gives the reader plenty of starting points for continuing their journey into algorithms and data structures.

I'm not big on rating things, but I had to for this amazing book. This is by far the best introduction to algorithms out there, especially if you have not encountered them before. If you're a dev new to coding from some other field and lack a CS background, start here. If you are a VISUAL LEARNER, start here. If you like light, easy text to get acquainted with an idea, start here. If you want to learn the basics and learn them well, start here. After you read this book you'll be ready for the more dense ones.

This is flattering and easy to use.

This eyeshadow is absolutely beautiful and stays on long. Great quality eyeshadow and looks even better in person. I am happy with this eyeshadow!

I bought the green and bronze eyeshadow colors and I am absolutely obsessed!! The shine and coverage is incredible! It dries fast and doesn’t feel cakey on your eyelids. I’ve worn other brands of liquid eyeshadow only to have it crack and flake off during the day, this formula does none of that! 8 hrs later, the eyeshadow looked exactly as vibrant and had the same coverage as when I had applied it that morning! I hope they come out with more colors, because this formula is perfection!

Great replacement for the older powder version.

First I am a fan of physicians formula as I have very sensitive skin. This particular product was too much glam for me and I unfortunately had to return it.

This makeup is made in China but I like that the company is transparent about that. When I looked at their manufacturer photos, they showed a close up of the front and back of the packaging, showing not just where it was made but also a full list of the ingredients. Their makeup quality is pretty great and this eyeshadow is no exception. Good pigment with a nice metallic sparkle. I think this line has diamond extract or something in it?? I'm not sure what the point of that is (sparkle? strength?), but if you're into crystals and stuff, that might be exciting for you (#noshade). I have the lip gloss from this line as well, and I really like it. Packaging is also very, very pretty.

I honestly thought it was going to be too dark for me, but I was pleasantly surprised. I really love the color. The pictures are taken from both inside and outside. This eyeshadow goes on smooth and doesn’t feel thick or heavy. I have use it both by itself and as a top layer for my regular eyeshadow and it looks great either way. I also haven’t noticed it creasing in my eyelids which is nice because most cream shadows do that for me. The packaging is really nice. The shadow itself is very shimmery which I love! It really only takes one sweep as long as you don’t want it to be super dark.

I’m a front end dev without any formal computer science education. In the past, when I heard terms related to algorithms and big O notation, they seemed scary and made me feel totally unqualified to be part of that world. But the reality is, a lot of these concepts seem more difficult than they really are. When they’re presented in simple terms with examples and illustrations, you start to realize that it’s not beyond you at all - you just haven’t learned it yet. This book does an incredible job of making these concepts accessible. I recommend it to anyone without a formal CS background, whether you’re new to programming, or like me, have been doing it for a long time but haven’t had much exposure to algorithms. I recently used this book as a resource when preparing for coding interviews, and it was invaluable. I give it a ton of credit for helping me land my current job in a role I didn’t think was possible for me just a few years ago.

Bang for your buck Shun. I've prepped meals for probably 150 hours with this thing so far and it's held up. Haven't had to sharpen it yet. Love it! The handle is light but not uncomfortable to the point where it messes with your confidence. Surgically sharp.Ignore the haters. People that are careless enough to drop their knives and/or knock them off the table shouldn't be leaving reviews about poor quality.

It's sharp, stays like that and at less than 7 Oz. it's really light.The knife was my first Shun, and I was quite impressed, until I accidentally dropped it. The knife fell from a height of about 3 feet (and as you know, I didn't try to catch it), and came apart.I really thought that for the price it would be a single piece knife, I was wrong.

This knife is razor sharp. I've never had a knife this sharp from the factory. In the image you can see that it cut a paper towel like it was card stock. Only very sharp knives can do this, and I've only done this with my own knives that I sharpened myself. The only down side to this knife is that it's not for beginners because it can break easily. It's very thin and has a high hardness, so it's gonna break if you drop it or chip if you're not careful.

I bought this for my husband as a birthday gift. He was a professional chef for 20 years but no longer has most of his good knives (they disappear easily in that world) and we love to cook at home. He has admired the various Shun knives at Williams-Sonoma over the years but has never wanted to spend the money on himself. The Sora appears to be a relatively new line for Shun and he absolutely loves this knife! Razor sharp, beautiful blade, and perfectly balanced, all as to be expected from Shun, and at a very affordable price. The handle is not as lovely as many of them but is very comfortable. I'll be buying more.

We got the small 4" Shun Sora and it became immediately apparent what a superior knife it was.Literally razor sharp and very easy to use.We got this one and I tried it out cutting up a 6 pound beef roast into 1/2" cubes.This is Normally a pain with our 10" Henckle.With this knife it was much, much easier and more accurate as well.It is also razor sharp and just glides through the meat.These are simply great knives and this one was very reasonably priced for a Sora.After this it is hard to imagine buying any other brand of knife.

OK I bought these on the recommendation of Alton Brown, who favors the Shun knife brand. And it turns out, I can see why I prefers these knives. The knives are super sharp, and stay that way even after extensive use. As long as you care for your Shun knife, it will last a long time and keep its edge very well. Now for the feel of the knife, it's weighted more on the blade, as the handle is very lightweight. So it is more front heavy, which is something you may or may not prefer. I prefer the weight distribution like this, as allows for easier guidance of the blade and for a strong forward slicing motion. But again, this is a matter of preference, and overall the knife is pretty lightweight as a whole.

This knife really is great for the price. I've owned several Shun and other Japanese knives and was looking for an Xmas present for my father in law who is a great cook but has never used one, typically sticking with American knives and a constant whetstone sharpening. He was immediately incredibly impressed with how sharp it was and it's now his knife of choice and for the price compared to other options out there it's an amazing value.

I am a Chef for Disney and I use this knife everyday I go into work or if I'm cokking at home. Comes very sharp and ready-to-use. I do like the speak design and is perfect for chopping, slicing, and mincing. Also has a very comfortable grip and extremely easy to get around the cutting board.

After going through a few different data structure and algorithm books and even part of Cracking the Coding Interview, and reading Grokking Algorithm's, I realize how much easier it is to get even the more difficult concepts by the way the author draws out examples. And when I saw draw out examples, there are illustrated examples that make it so simple even a kid can get these concepts that even experienced developers struggle with. Also, where other authors fail, Adit includes key insights about why and when to use certain algorithms and data structures that just make sense. Even if you don't understand Python (most simplified/elegant language I've seen so far), this book is a must read if you're interested in interviewing or even if you're just trying to gain a better understanding of data structures and algorithms.

It makes learning algorithm fun. It covers the most popular algorithms and use cute pictures to explain the point. I was borrowing this from my local library a few times and then decided that it's worth the purchase because I keep referring back to it.I think the hard copy version has a nicer layout that makes it very comfortable to read. I didn't like the e-book version as much.

The author patiently explains what can easily become a nightmare to learn

Writing and teaching about algorithms is hard. And books on algorithms tend to cater to one audience, your typical programmer who most likely has a computer science degree and took a semester on Algorithms. And these books fall in to two camps: academicky/theoretical and highly technical for job interviews. The most popular books on algorithms are not beginner friendly.Grokking Algorithms is the opposite. And caters to an unmet need: newbies and programmers who haven't touched algorithms for a long time.The book does three things well. One, it has well-drawn and simple pictures and diagrams to explain foundational algorithms such as QuickSort or Binary Search. Two, the explanations for harder algorithms such as Dijkstra's Algorithm, Greedy Algorithms, and Dynamic Programming are excellent. For a programmer who hasn't done algorithms in a long time, the author's explanations were simple, easy-to-understand, and enabled me to remember key ideas from my college days. Three, the organization of the topics enabled me to use previous chapters to understand new algorithms and emphasize key ideas that I learned early in the book. Pro-tip: read this book in order, especially if you're a newbie.Some negatives: The illustrations are hand-drawn, so sometimes it's hard to understand what the captions or diagrams are showing. If you don't know what an illustration is supposed to say, turn the book and view the illustration from a different angle. Sometimes, the words are written sideways from bottom to top. Also, the book loses focus in chapters 10 and 11. It no longer focuses on algorithms. Instead, the author writes about machine learning, encryption algorithms, and lesser known algorithms. The book could easily have stopped at chapter 9. I honestly think that these chapters were filler chapters. If you're reading this book to prepare for a job interview at a tech company, stop at chapter 9.

The Hanes Mens Tagless Boxer is a fantastic product that exceeded my expectations. The material is incredibly soft and comfortable, with a perfect blend of cotton and polyester. The tagless design eliminates any itching or scratching that can come with traditional tags, making these boxers a pleasure to wear all day long. The fit is true to size, and the waistband stays in place without rolling or bunching up. Additionally, the price point is very reasonable and the quality of the product is great. I would highly recommend these boxers to anyone looking for a comfortable and affordable option. Overall, I am very satisfied with this product and will definitely be purchasing more in the future.

Like most everything, they are thinner than the last ones I bought, which happened to be at an outlet store. I usually buy a good QUANTITY of items like this so I don’t have to wash so often. I tried to buy the ones I had bought originally from the outlet store a while back but, the size price and quality made me pass on them. Since my original ones, the bands are getting weak on some, I said it’s time to try some here. Overall, I like them. Initially I bought one pack. There were threads hanging off one pair and I believe they will be the first to go. I decided not to return them because, I imagine I need to send them all back, not just one pair, therefore the next pack could have a pair that’s questionable. So I ordered another pack and all was good. It’s nice to have new undies. Ive lowered my expectations on things like these because as you know, one pair can easily be $20 in a quality Mens store. Bottom line like I said is, I like them and would recommend them if you agree with what to expect. Thanks!

I’ve been buying expensive Polo boxers for many years because most inexpensive ones were just uncomfortable. I got these on a Black Friday sale and 6 pairs less than 1 pair polos. They’re great so no more expensive boxers as long as Hanes keeps making these

I have always worn traditional Hanes boxers that seem to be harder to find these days due to such a variety of underwear being offered these days. Amazon never fails they had exactly what I wanted and the price was the best and of course Amazon shipping options are the best in the business. I am completely satisfied with my purchase.

My husband had been struggling w other name brand stores boxers shrinking in the dryer after a short period of time. These are the best I’ve found with a variety of colors, styles.He’s tossed all of his old ones and upgraded to these. Very happy with decision.

...A pair of boxers that don't creep up my butt. This has been my biggest hatred of boxers, but that's not the case with these. They fit well, they don't "ride" up, and they're not binding. The seam design in the back is what makes them work well.

My husband decided to give boxers a try, and is very happy with the Hanes boxers.These fit as expected, and he took his normal size for a perfect fit.Soft ,breathable cotton make these very comfortable, and hubbyespecially likes the lack of tags, and the good leg room.These boxers are definitely more for comfort, versus (ahem) "support".For that, a different style would be better.Good product, and great value with Hanes.

It can be hard to find well fitting undies as a larger fellow, but these are really the best I've had. They don't bunch or roll and the elastic holds well. The fabric is thin and breathable but doesn't feel cheap, never feels like it's not gonna hold my body size. Great especially 3X expectations of fit.

Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People
⭐ 4.6 💛 1249
paperback: $27.71
Buy the Book