AI Hints
Home›DSA in Python Tutorial›Getting Started
DSA in Python · Lesson 2 of 132

What Is an Algorithm?

Steps to solve a problem.

⏱ 2 min read▶ Video at 1:11🎓 Free
Watch this lesson in the DSA in Python courseWatch on YouTube ↗Full DSA in Python course

An algorithm is a list of steps that solves a problem. A recipe is an algorithm. So are the directions to a friend's house. In programming, an algorithm is the steps your code follows, to turn an input into an answer.

What Is an Algorithm?
From the video: What Is an Algorithm?
  • Steps that solve a problem
  • A recipe, directions
  • Input in, answer out

Finding the biggest number

Here's a tiny algorithm. Find the biggest number in a list. We start by assuming the first number is the biggest. Then we look at each number in turn. If it's bigger than our best so far, it becomes the new best. Run it. Nine is the biggest.

biggest.pypython
def biggest(numbers):
    best = numbers[0]
    for n in numbers:
        if n > best:
            best = n
    return best

print(biggest([4, 9, 2, 7]))
Output
9

Good algorithms

There's usually more than one way to solve a problem. A good algorithm gives the right answer every time, including for strange inputs, like an empty list. It finishes in a reasonable time, even for huge inputs. And it doesn't waste memory. This course is about choosing well.

Correctevery time, every input
Fasteven for huge inputs
Leandoesn't waste memory
Clearothers can read it

Summary

An algorithm is a list of steps that turns an input into an answer. Good ones are correct, fast, and lean. Next, why this matters so much.

Key takeaways
  • Steps to solve a problem
  • Correct, fast, lean
  • Many ways to solve one problem
Next up: Why It Matters — speed at scale