In the previous lesson, you learned that data is the fuel for AI. But fuel alone does not move a car - you need an engine. In AI, that engine is an algorithm. Every time an AI system makes a prediction, recommends a video, or detects spam, an algorithm is doing the heavy lifting behind the scenes.
An algorithm is simply a step-by-step set of instructions for solving a problem. You follow algorithms every day without realising it.
The key idea is that an algorithm must be precise, ordered, and repeatable - anyone following the same steps should get the same result.
The word "algorithm" comes from the name of a 9th-century Persian mathematician, Muhammad ibn Musa al-Khwarizmi, who wrote one of the first books on systematic problem-solving.
Before we dive into AI, let us look at two simple algorithms you already understand intuitively.
When you pick up a hand of playing cards and arrange them in order, you are running a sorting algorithm:
This is essentially how bubble sort works - one of the simplest sorting algorithms in computer science.
You do not start at page one and read every word. Instead, you:
Sign in to join the discussion
This is called binary search, and it is incredibly efficient. It can find any word among a million entries in about 20 steps.
Why is binary search faster than reading every entry from the start?
AI algorithms are more sophisticated than sorting cards, but the principle is the same: follow structured steps to reach an answer. Here are two foundational AI algorithms.
A decision tree asks a series of yes-or-no questions to classify something.
Example - Is this email spam?
Each question is a branch, and each final answer is a leaf. Decision trees are easy to understand, which makes them popular when humans need to explain the AI's reasoning.
KNN classifies something by looking at the closest examples it has already seen.
Imagine you move to a new neighbourhood and want to know if a house is expensive or affordable. You look at the five nearest houses (your "neighbours") and check their prices. If most are expensive, you predict yours is too.
KNN works exactly the same way with data points - it finds the K closest examples and takes a vote.
If you asked three friends for a film recommendation and two of them suggested the same film, you would probably watch that one. That is the core idea behind KNN - majority rules among your nearest neighbours.
In a K-Nearest Neighbours algorithm with K=5, how does the model make its prediction?
Not all algorithms are equally fast. Computer scientists use Big O notation to describe how an algorithm's speed changes as the data grows.
| Notation | Name | Example | Speed | |----------|------|---------|-------| | O(1) | Constant | Looking up a value by index | โก Instant | | O(log n) | Logarithmic | Binary search | ๐ Very fast | | O(n) | Linear | Reading every item in a list | ๐ Decent | | O(nยฒ) | Quadratic | Comparing every item to every other | ๐ข Slow |
You do not need to memorise the maths. The key insight is this: as your dataset grows, a poorly chosen algorithm can go from fast to impossibly slow.
Google processes over 8.5 billion searches per day. If their search algorithm were O(nยฒ) instead of highly optimised, a single search could take hours instead of milliseconds.
Big O notation is not about exact speed - it is about how speed scales. An O(n) algorithm might be slow on tiny data but will always outperform an O(nยฒ) algorithm as the dataset grows large.
There is no single "best" algorithm. The right choice depends on:
A decision tree might be perfect for a simple loan approval system where transparency is required. But for recognising objects in a photo, you would need a neural network - which we will explore in the next lesson.
A hospital needs an AI to help diagnose patients. Should they choose an algorithm that is highly accurate but impossible to explain, or one that is slightly less accurate but shows its reasoning clearly? What are the trade-offs?
Which factor is LEAST important when choosing an AI algorithm?
Next, we will look at the most powerful family of algorithms in modern AI: neural networks.