What is an Algorithm?

⏱ 8 min✏️ Quiz at the end

Instructions Are Everywhere

Before you ever touch a computer, you already use algorithms every day. An algorithm is simply a set of step-by-step instructions for solving a problem or completing a task.

  • A recipe tells you how to turn ingredients into a meal
  • Directions tell you how to get from home to school
  • Furniture instructions tell you how to turn a box of parts into a bookshelf

Each one takes an input (ingredients, a starting point, parts), follows steps in order, and produces an output (a meal, arriving at school, a bookshelf).

What Makes an Algorithm Good?

Not every list of instructions is a good algorithm. A good algorithm is:

PropertyMeaning
ClearEach step is precise β€” no guessing needed
OrderedSteps happen in a logical sequence
FiniteIt finishes after a limited number of steps
CorrectFollowing it actually solves the problem

"Add some flour" is a bad step β€” how much is some? "Add 200 g of flour" is a good step.

Why Computers Need Algorithms

Here is the most important idea in all of computing:

A computer does exactly what it is told β€” nothing more, nothing less.

A computer has no common sense. If a step is missing, it will not fill in the gap. If a step is vague, it cannot guess what you meant. This is why instructions for a computer must be precise and unambiguous.

Try this classic exercise: write instructions for making a jam sandwich, then follow them exactly, like a robot would. Did you remember to say "open the jar"? "Pick up the knife"? Most first attempts fail β€” that is what programming a computer feels like!

One Problem, Many Algorithms

The same problem can usually be solved in more than one way.

Problem: find a friend's name in a list of 1000 names.

  • Algorithm A: check every name one by one from the top.
  • Algorithm B: if the list is alphabetical, open it in the middle, decide which half the name is in, and repeat.

Both work β€” but Algorithm B needs about 10 checks while Algorithm A might need 1000. Choosing a better algorithm is often more powerful than buying a faster computer.

Ways to Write an Algorithm

An algorithm is an idea, and there are several ways to write it down:

  1. A numbered list in plain language β€” like a recipe
  2. A flowchart β€” a diagram of boxes and arrows showing the steps and decisions
  3. Pseudocode β€” structured, code-like English such as IF raining THEN take umbrella

No programming language is needed. In these lessons we will use plain steps, flowcharts, and simple pseudocode.

The Building Blocks

Almost every algorithm β€” from a recipe to a game β€” is built from just three ingredients:

  • Sequence β€” do the steps in order, one after another
  • Selection β€” make a decision: IF something is true, do this, otherwise do that
  • Repetition β€” repeat steps until a condition is met

Master these three, and you can describe any process a computer can perform.

Worked Example β€” Designing an Algorithm From Scratch

Suppose the task is: given a list of exam marks, work out how many students passed (a pass is 40 or above). Watch the full design process, start to finish:

  1. Identify the input and output. Input: a list of marks. Output: a single number β€” the count of passes.
  2. Identify the building blocks needed. Visiting every mark in the list needs repetition; deciding "did this one pass?" needs selection; keeping a running count needs a variable.
  3. Write it as pseudocode:
SET passCount = 0
FOR EACH mark IN marks
    IF mark >= 40 THEN
        SET passCount = passCount + 1
    ENDIF
NEXT
OUTPUT passCount
  1. Trace it with a small example, marks = [55, 32, 78, 40, 21], to confirm it actually works: passCount starts at 0, becomes 1 (55 passes), stays 1 (32 fails), becomes 2 (78 passes), becomes 3 (40 passes β€” the boundary case matters here, since >= includes exactly 40), stays 3 (21 fails). Final output: 3.

This four-step process β€” identify input/output, choose building blocks, write it down, then trace it against an example β€” is the same process behind every algorithm in this course, however complicated it eventually becomes.

Algorithms Exist Independently of Any Computer

It is worth returning to a point made earlier, because it is easy to forget once code enters the picture: an algorithm is a plan, and a computer is just one possible way to carry that plan out. The passing-marks algorithm above could be performed by a human with a highlighter and a calculator, by a spreadsheet formula, or by code in any programming language β€” the algorithm itself does not change, only who or what executes it. This is precisely why this course spends so much time on plain steps, flowcharts, and pseudocode rather than jumping straight to a specific coding language: the thinking transfers everywhere, while the exact syntax of one language does not.

Key Words

  • Algorithm β€” a precise, step-by-step set of instructions to solve a problem
  • Input β€” the information the algorithm starts with
  • Output β€” the result the algorithm produces
  • Unambiguous β€” having exactly one meaning; impossible to misunderstand
  • Pseudocode β€” algorithm steps written in structured plain English, not a real programming language