10 Algorithms Every Programmer Should Know – and When to Use Them

What’s an algorithm? Good question! In my academic days, we would have said, “An algorithm is a well-defined, self-contained process or set of rules to be followed in a data processing system.”

But this isn’t the university, and that was pretty much word salad, so here’s the deal:

An algorithm is a recipe for a solution to a problem.

The great thing about recipes is that we can repeat them and adapt them for our own use. Want cookies? Great, get your favorite cookie recipe! What if you want chocolate-chip cookies? Add chocolate chips! What if you want peanut butter cookies… you get the idea.

Of course, some recipes don’t give us what we want. It would be silly to use a recipe for cookies when you really want clam chowder. It’s important to use the right algorithm to solve the problem.

Story time! I went to a friend’s wedding reception a few years ago, and they served a vegetable side dish that was delicious. All I can remember is that it had broccoli in it. I don’t know what the sauce was, or how it was cooked….and I’d love to have it again, or even learn to make it, but I don’t know what it was called. I don’t even know what to search for to figure out what recipe to search for. “OK Google, find me a recipe for that really good broccoli stuff from Dan’s wedding.”

Programmers sometimes find themselves in the same situation: We’ve got a problem, we think there’s probably already a good way to solve this, but we’re not sure what it is or how to even go about looking for it.

I don’t even know what to Google.

There are thousands of well-defined algorithms out there, but here’s the secret: The majority of problems in real-world software can be solved with just a handful of algorithms, and if your problem can’t be solved by these, chances are these are the first step towards finding the right algorithm for your problem anyway.

So, here are Professor Jake’s top 10 algorithms every programmer should know, and when to use them:

Use it when…

How it works:

You have a list of things on disk, or other “slow storage” that need to be in order, but aren’t.

Break the list into two lists. Then break each of those into two lists, etc… until each list is only 1 or 2 items. If the two things in those tiny lists aren’t in order, swap them. Then combine two tiny lists together in order, then combine those lists together in order, etc… until you’re left with one list that’s in order.

Use it when…

How it works:

You have a list of things in memory that need to be sorted, but aren’t.

Pick an item in the list to be the “pivot.” Put all the items that are less than the pivot on one side of the pivot, and all the other items on the other side. Then break the list into two smaller lists at the pivot. Repeat for each smaller list. Once all the lists have only one item, combine them back together and they’re in order.

Use it when…

How it works:

Use it when…

How it works:

You have a list of things that are in order, and you need to find one of them.

Look at the middle item in the list. If it’s your item, then you’re done! Otherwise, take the half-list where your item would be and repeat by finding the middle item of that half-list. Repeat until you find the item, (or there are no half-lists left, in which case your item isn’t there).

Use it when…

How it works:

You have a bunch of “places” and “paths”, and need to find the shortest path between two places.

Make a list of all the places. Beginning at your ending place, put a “1” next to each place that’s connected to the ending place by a path. Then, for each of those places, do the same, but this time put a “2”. Continue looking at each place and all the places it can get to, putting the number that indicates how far it is from the ending place by adding 1 to the connected place’s distance. If a place already has a number, you replace it if the new number would be smaller. When you’ve looked at all the places, the path from the starting place to the ending place will follow the smallest numbers.