Definition
Could a robot make tea from your instructions, exactly as written?
An algorithm is a finite sequence of precise, unambiguous steps that takes some input and produces the required output. A good algorithm is correct (it always gives the right answer), complete (it covers every case) and finishes in a finite number of steps.
In simple words
"Make tea" is not an algorithm, because a robot doesn't know what to do. "Fill the pan with one cup of water, heat until it boils, add one spoon of tea leaves…" is closer. Every programmer first works out the steps, then writes them in a programming language. The language changes; the thinking doesn't.
Overview
- An algorithm is a finite, ordered list of clear steps that turns given inputs into the wanted output.
- Every good algorithm is precise (no guessing), finite (it stops), and correct for every valid input.
- The journey is: understand the problem, list inputs and outputs, break it into sub-problems, write steps, test.
- The same problem can be solved by different strategies, and some are far faster than others.
- Algorithms are written first in plain words or pseudocode, and only then in a programming language.
- Every AI model is trained and used through algorithms, from sorting data to gradient descent.
Why it matters
Most programming bugs come from unclear thinking, not from typing. Writing the algorithm first, in plain words, shows the gaps before any code exists. Every AI method in this course, from search to neural networks, is an algorithm.
Key terms
- Algorithm
- A finite sequence of precise steps that turns an input into the required output.
- Input
- The data an algorithm starts with.
- Output
- The result an algorithm produces.
- Correctness
- Always producing the right output for every valid input.
- Edge case
- An unusual or extreme input, such as an empty list or equal values, where algorithms often fail.
- Problem decomposition
- Splitting a problem into smaller sub-problems that are easier to solve.
Types
Algorithms are often grouped by the strategy they use to solve a problem. These five design approaches cover most algorithms you will meet.
1. Brute force
Trying every possible answer and keeping the one that works or is best.
- How it works: It lists all candidates systematically and checks each one, so it is simple and always correct, but the work grows very fast with input size.
- Example: Finding a 4-digit lock combination by trying 0000, 0001, 0002 and so on up to 9999.
- When to use it: Use it for small inputs, as a correct baseline to compare faster methods against, or when nothing smarter is known.
2. Divide and conquer
Splitting a problem into smaller copies of itself, solving those, and combining the answers.
- How it works: It splits the input (often in half), solves each part recursively until the parts are tiny, then merges the partial results.
- Example: Merge sort splits a list in halves, sorts each half, then merges the two sorted halves.
- When to use it: Use it when a problem breaks naturally into independent pieces, such as sorting, searching sorted data or multiplying large numbers.
3. Greedy
Making the choice that looks best right now at each step, without going back.
- How it works: At every step it picks the locally best option (the biggest coin, the nearest city) and never reconsiders; this is fast but not always optimal.
- Example: Paying ₹68 with the fewest Indian coins and notes: take ₹50, then ₹10, then ₹5, then ₹2, then ₹1.
- When to use it: Use it when a local best choice is proven to lead to the global best, as in making change with standard coins or building minimum spanning trees.
4. Dynamic programming
Solving overlapping sub-problems once each and storing their answers to reuse them.
- How it works: It builds a table of answers to smaller sub-problems, from the smallest up, so no sub-problem is solved twice.
- Example: Computing the 50th Fibonacci number by storing each earlier value instead of recomputing it millions of times.
- When to use it: Use it when the same smaller questions keep coming up, as in shortest paths, the knapsack problem and edit distance between words.
5. Backtracking
Building a solution step by step and undoing a step as soon as it leads to a dead end.
- How it works: It tries a choice, moves forward, and if a rule is broken it goes back and tries the next choice, pruning hopeless paths early.
- Example: Solving a Sudoku: place a digit, continue, and erase it when a row, column or box clashes.
- When to use it: Use it for puzzles and constraint problems such as Sudoku, N-queens, timetables and finding all valid arrangements.
Explained in detail
From a vague task to precise steps
Most real tasks arrive vague: "find the topper", "check if this password is strong", "suggest a route". Turning them into an algorithm means answering three questions. What exactly is the input, and in what form? What exactly should the output be? And what steps transform one into the other, for every possible input? For "find the topper", the input might be a list of names and marks, and the output the name with the highest mark. But what if two learners tie? What if the list is empty? An algorithm is only finished when it has an answer for those cases too.
Properties of a good algorithm
| Property | Meaning | How to check |
|---|---|---|
| Precise | Each step has exactly one meaning | Could a machine follow it without guessing? |
| Finite | It always stops | Does every loop have a condition that will become false? |
| Correct | Right output for every valid input | Test normal and edge cases |
| Complete | Handles all situations | List unusual inputs and walk through each |
| Efficient | Uses reasonable time and memory | How does the work grow as the input grows? |
A worked example: is a year a leap year?
The rule sounds simple, "every fourth year", but the full rule from the Gregorian calendar has three parts. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not, except that years divisible by 400 are. So 2024 is a leap year, 1900 was not, and 2000 was. Written as an algorithm: if the year is divisible by 400, answer yes; otherwise, if divisible by 100, answer no; otherwise, if divisible by 4, answer yes; otherwise, answer no. Notice that the order of the checks matters. Checking "divisible by 4" first would wrongly call 1900 a leap year.
Thinking before typing
- Solve two or three small examples by hand and notice what you did.
- Write those actions as numbered steps in plain language.
- Look for repetition (a loop) and choices (a condition).
- List edge cases: empty input, one item, ties, negative numbers, very large values.
- Only then translate the steps into Python.
Professional programmers do this constantly. The famous computer scientist Donald Knuth defined an algorithm with five properties, finiteness, definiteness, input, output and effectiveness, in his book The Art of Computer Programming, first published in 1968. The ideas are the same ones you are using here.
How it works
Input, steps, output
| Part | Largest of three numbers |
|---|---|
| Input | Three numbers a, b and c |
| Steps | Assume a is largest. If b is bigger, b becomes largest. If c is bigger than that, c becomes largest. |
| Output | The largest number |
How to check an algorithm
- Is every step precise enough that a machine could follow it without guessing?
- Does it handle every case, including ties and empty inputs?
- Does it always stop?
Try it yourself
Write the steps to find the largest of three numbers
Put the steps in an order that always works.
- 1Read the three numbers a, b and c
- 2If c is greater than largest, set largest to c
- 3Output largest
- 4Set largest to a
- 5If b is greater than largest, set largest to b
Precise or vague?
Could a machine follow each instruction without guessing?
Repeat 10 times: add 1 to count
If the mark is 40 or more, output PASS, otherwise output FAIL
Add a little salt
Sort the list nicely
Common mistakes
- Starting to type code before knowing the steps.
- Forgetting edge cases, such as two equal numbers or an empty list.
- Writing steps that only a person could follow, such as 'pick a good one'.
Check yourself
Summary
- An algorithm is precise, complete steps from input to output that always finish.
- Write it in plain words first; translate it into a language later.
- Check it for precision, every case, and stopping.
Finished reading?
Sources: W3Schools DSA with Python