DEV Community

Cover image for What is an Algorithm? Explained with Everyday Examples
Raaga Priya Madhan
Raaga Priya Madhan

Posted on

What is an Algorithm? Explained with Everyday Examples

The word "algorithm" sounds intimidating. It's not. You follow algorithms every day without realizing it.

An algorithm is just a set of steps

When you make tea, you follow an algorithm:

  1. Boil water
  2. Add tea bag to cup
  3. Pour hot water into cup
  4. Wait 3 minutes
  5. Remove tea bag
  6. Add milk and sugar if needed
  7. Drink

That's it. A fixed set of steps that produces a result. Algorithms in programming work exactly the same way.

A real programming example

Say you want to find the largest number in a list. Here's the algorithm:

numbers = [4, 7, 2, 9, 1]

largest = numbers[0]

for number in numbers:
    if number > largest:
        largest = number

print(largest)
Enter fullscreen mode Exit fullscreen mode

Step by step:

  1. Assume the first number is the largest
  2. Go through every number in the list
  3. If a number is bigger than your current largest, update largest
  4. Print the result

Output: 9

Why algorithms matter

Every app you use runs on algorithms. Google's search results, Instagram's feed, Spotify's recommendations — all algorithms deciding what to show you.

The two things every algorithm needs

  • Correctness — it must produce the right answer
  • Efficiency — it must do it without wasting time or memory

The one-line summary

An algorithm is a step-by-step recipe for solving a problem. Every program you've ever used runs on them.


Written by Raaga Priya Madhan — CSE student, Bangalore. I write about CS concepts simply. Connect with me on LinkedIn

Top comments (0)