Let's Learn Something! Algorithms Explained in 5 Levels – From Peanut Butter Sandwiches to Artificial Intelligence

Visual representation of algorithms from simple tasks to AI neural networks

Written by Massa Medi

By David J. Malan, Harvard University Professor of Computer Science

In today's technological landscape, the word algorithm is everywhere: powering search engines, customizing your social media feed, and even lurking in the background as you unlock your phone. But what really is an algorithm? How does it shape the modern (and even not so modern) world? Join Harvard Professor David J. Malan as he tackles the concept of algorithms in five levels of escalating complexity, illuminating how these powerful sets of instructions underpin everything from making a peanut butter sandwich to training the world’s most advanced artificial intelligences.

Level 1: Algorithms for Everyday Life

To begin, let's step away from the digital world and think in more familiar terms. As Professor Malan introduces himself, he highlights an encouraging idea: algorithms are everywhere not just inside computers and smartphones, but in daily routines we often overlook.

Imagine preparing for bedtime. There’s a sequence, right? Get dressed, brush your teeth, read a story, then sleep. That, in its essence, is an algorithm: a precise list of instructions to solve a specific problem in this case, transitioning from “awake and energetic” to “peacefully asleep.”

Making a Sandwich A Real World Algorithm in Action

To illustrate how specifying steps matters, Professor Malan and a young helper play a game: “Pretend I’m a robot, and you program me to make a peanut butter sandwich.”

Their instructions go something like this:

  1. Open the bag of bread.
  2. Grab the bread and put it on the plate.
  3. Take all the bread back and put it back in there. (Oops! An undo command, like Ctrl+Z.)
  4. Take one bread and put it on the plate.
  5. Take the lid off the peanut butter jar, put the lid down.
  6. Take a knife, dip it in the peanut butter, and spread it on the bread.
  7. Place another slice of bread on top, flat.
  8. Take a delicious bite!
  9. Finally, clean up the mess. (Because even robots should be tidy.)

Through this playful exercise, Malan demonstrates why precision and clarity in algorithms are crucial. Miss a step, or phrase it ambiguously, and things might go (hilariously) wrong such as accidentally pulling out all the bread instead of just one slice. The very same principle is what guides computers, which require step by step, unambiguous, and often painfully specific instructions to function properly.

Level 2: Searching for John in the Phone Book – The Art of Algorithmic Search

Now, shift your attention to a scenario familiar to both past and present generations: searching for a contact. Why does your phone organize contacts alphabetically? Because searching through an unorganized list is an inefficient algorithm: you’d have to scroll all the way to the bottom even if your friend John’s name is there.

Professor Malan recalls the phonebook era a time when every household had a thick directory of names and numbers, alphabetized for efficiency. Suppose you’re searching for “John.” There are several strategies (algorithms):

These search strategies are more than just theory: your smartphone (and Google itself) use advanced versions of “divide and conquer” every time you search for a name, a website, or your favorite song. This ability to systematically break down a problem into smaller parts is at the heart of algorithmic thinking and efficiency.

Level 3: Sorting the Chaos – Bubble Sort and Beyond

How do we turn a jumble of numbers or names into an ordered, easily searchable list? Patricia, a senior at NYU, remembers learning about bubble sort in her first computer science classes.

Here’s how it works, step by step, using magnets on a chalkboard to represent numbers:

  1. Start with a row of unsorted numbers (e.g. 8, 1, 2, 6, 3, 4, 7, 5).
  2. Compare the first two numbers. If they’re in the wrong order (e.g. 8, 1), swap them.
  3. Move to the next pair (now 8, 2). Keep swapping any out of order pairs, one by one, until you reach the end of the list.
  4. The largest number has "bubbled" to the end.
  5. Ignore the last item (since it’s now sorted), and repeat the process for the rest over and over, shrinking the unsorted portion each time.

Gradually, everything falls into place from smallest to largest. This is simple but not always fast compared to more advanced sorting algorithms. Still, it’s a perfect demonstration of fixing “local” small problems to organize the bigger picture.

While the world’s biggest companies like Google or Bing don’t use bubble sort on the entire internet (the scale would be impossible!), they rely on underlying algorithms to organize and retrieve information efficientlyjust as we do in our daily routines.

Level 4: Algorithms Behind Social Media Feeds and AI Recommendations

Think about your For You page on TikTok, or the next show that pops up on Netflix these aren't random suggestions. They’re the result of highly sophisticated algorithms, often powered by machine learning and artificial intelligence.

These algorithms take into account every video you watch, each post you like, and even what your friends are engaging with. They don’t just follow if/then statements written by human coders. No one at TikTok is typing explicit instructions like, “If Patricia likes post X, show her post Y.” Instead, advanced models digest immense amounts of data, spot patterns, and make recommendations to keep users engaged (and platforms profitable).

These are learning algorithms capable of adjusting and getting better over time as they’re exposed to more data. Techniques like neural networks and reinforcement learning allow these systems to “learn” what you want without needing explicit instructions for every possible user action.

Algorithms in Research and Industry

The reach of algorithms doesn't stop at entertainment and search. As an NYU PhD student specializing in robot learning describes, innovations in algorithms are driven by inefficiency: "How could this process be made faster, smarter, more effective?” In robotics and AI, old school rule based systems (e.g., A* search for games) work well in limited domains. But as systems become more complex, learning algorithms those that adapt and optimize based on growing datasets now dominate, from board games like Go to routing trains across entire cities.

Modern society is awash in algorithms: from transit routes to financial data, online advertising to logistics. But while this brings enormous efficiency and customization, it also means trade offs: increased data mining, new privacy concerns, and the subtle shaping of our online experiences.

As algorithms become more pervasive and more advanced even experts note the line between “good” and “bad” consequences isn’t always clear. Ultimately, it’s about balance: using algorithms to make life better, while understanding their limitations and impact.

Level 5: The Mystery and Future of AI Algorithms

According to Chris Wiggins, Associate Professor of Applied Mathematics at Columbia and Chief Data Scientist at The New York Times, data science teams are responsible for crafting and integrating algorithms that quietly power everything from personalized recommendations to workflow efficiency.

In modern data science, an algorithm might mean an optimization method for building the best statistical model or even a complete “data product” integrated into business platforms or newsrooms. But as machine learning (ML) and AI explode in popularity, the field is rapidly evolving:

With the recent advances and splashy successes of AI from chatbots to deepfakes the world is confronting both new potential and new risks. Should you steer away from a career in computer science just because of these breakthroughs? Absolutely not! The fundamentals of algorithms remain central: training, fine tuning, and optimizing these models are themselves algorithmic challenges.

AI technology is neither inherently good nor bad, as Kranzberg’s First Law of Technology reminds us: “Technology is neither good nor bad, nor is it neutral.” How we develop, understand, and use these tools will shape the world to come.

From Bedtime Routines to Complex AI: The Algorithmic Journey

Whether you're teaching a child to make a sandwich, searching for a friend in a contacts list, building game winning strategies, or deploying the latest neural networks, algorithms underpin it all.

For those just beginning their own journey, the endgame those dazzling feats of AI might feel far away. But with each concept learned and every small algorithm mastered, that world draws closer.

The next time you swipe, search, or ask an AI for help, remember: there are layers of ingenious algorithms making it all possible. And each step forward is a testament to the creativity and precision of the human mind.

Recommended Articles

Hey there! This is Merge Society. We'd love to hear your thoughts - leave a comment below to support and share the love for this blog ❤️