AiTechWorlds
AiTechWorlds
Imagine you're cooking pasta for the very first time. You search online and find a recipe:
This recipe is an algorithm. It's a step-by-step set of instructions that, when followed precisely, produces the same result every time — regardless of who follows it.
Now imagine cooking that same pasta for 1000 people. Suddenly, the recipe isn't enough. You need to think about how many pots, how to coordinate timing, which steps can happen in parallel. A good algorithm doesn't just work for one case — it scales.
That's the essence of what computer scientists care about. Not just "does it work?" but "does it still work when the input is 10x, 100x, or 1,000,000x bigger?"
An algorithm is a finite sequence of well-defined instructions that solves a class of problems and produces a correct output for every valid input.
The key word is class. A sorting algorithm doesn't sort one specific list — it sorts any list. A pathfinding algorithm doesn't find one route — it finds the shortest path in any graph.
| Property | Meaning | Example |
|---|---|---|
| Input | Zero or more inputs are given | A list of numbers to sort |
| Output | At least one output is produced | The sorted list |
| Definiteness | Each step is precisely and unambiguously defined | "Sort" is vague; "compare adjacent elements" is definite |
| Finiteness | The algorithm must terminate after a finite number of steps | An infinite loop is not an algorithm |
| Effectiveness | Each step must be basic enough to be carried out | Steps must be executable operations |
Vague instruction:
"Sort these numbers: 5, 2, 8, 1"
This tells you what to do but not how. A computer cannot execute this.
Real algorithm (Bubble Sort outline):
1. Start at the beginning of the list
2. Compare the first and second element
3. If the first is greater than the second, swap them
4. Move to the next pair and repeat step 3
5. After one pass, the largest element is at the end
6. Repeat steps 1–5 for the remaining unsorted portion
7. Stop when no swaps occur in a full pass
This is unambiguous. Every step is executable. It terminates. It works on any list.
The ancient Greek mathematician Euclid described an algorithm for finding the Greatest Common Divisor of two numbers around 300 BC. It's still used today.
The insight: The GCD of two numbers doesn't change if you replace the larger number with its remainder when divided by the smaller.
def gcd(a, b):
# Base case: if b is 0, GCD is a
while b != 0:
a, b = b, a % b # Replace a with b, b with remainder of a/b
return a
# Example usage
print(gcd(48, 18)) # Output: 6
print(gcd(100, 75)) # Output: 25
Trace for gcd(48, 18):
Step 1: a=48, b=18 → new a=18, new b=48%18=12
Step 2: a=18, b=12 → new a=12, new b=18%12=6
Step 3: a=12, b=6 → new a=6, new b=12%6=0
Step 4: b=0, return a=6
Output: 6
This algorithm runs in O(log(min(a,b))) time — extraordinarily fast. A 2300-year-old idea, still in every modern math library.
These two words are often confused. They are not the same thing.
An algorithm is the idea. A program is the implementation.
| Concept | Algorithm | Program |
|---|---|---|
| What it is | Abstract set of instructions | Concrete code in a specific language |
| Language | Language-independent | Written in Python, Java, C++, etc. |
| Runs on | Paper, whiteboard, mind | A real computer |
| Example | "Divide and conquer to sort" | sorted() in Python |
The same algorithm can be implemented in dozens of programming languages. The algorithm itself doesn't care about syntax — it's a logical plan.
Algorithms and data structures are partners. You almost never choose one without thinking about the other.
| Problem | Algorithm | Data Structure |
|---|---|---|
| Find shortest path in a map | Dijkstra's Algorithm | Graph (adjacency list) |
| Search a sorted list | Binary Search | Sorted Array |
| Autocomplete suggestions | Trie traversal | Trie (prefix tree) |
| Rank web pages | PageRank | Directed Graph |
| Undo/redo in an editor | Stack operations | Stack |
The right algorithm applied to the wrong data structure can be 1000x slower than the right combination.
Algorithms aren't just academic exercises. They power the digital world:
There are two questions to ask about any algorithm:
1. Is it correct? Does it always produce the right answer for every valid input? An algorithm that usually works but sometimes fails is not an algorithm — it's a bug.
2. Is it efficient? Does it run fast enough, and use acceptable memory, even for large inputs?
A correct but slow algorithm is usable for small inputs. An efficient but incorrect algorithm is useless. You need both.
In this course, we'll study algorithms from both angles — proving they work, and analyzing how fast they work as inputs grow.
In the next lesson, we'll learn exactly how to measure efficiency — using Big O notation.
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises