🧩beginner
What Are Algorithms?
Understand what algorithms are, why they matter, and how to think about solving problems step by step.
Algorithm = Recipe
An algorithm is just a set of steps to solve a problem. You already use algorithms every day without knowing it:
• A recipe is an algorithm for making food
• Google Maps uses an algorithm to find the fastest route
• Spotify uses an algorithm to recommend songs
• Your brain uses an algorithm to decide what to wear
In programming, an algorithm is a step-by-step solution written in code.
Why Algorithms Matter
Two pieces of code can solve the same problem, but one might be 1000x faster. That's the difference a good algorithm makes.
Imagine searching for a name in a phone book:
• Bad algorithm: Start at page 1, check every name one by one. Could take hours.
• Good algorithm: Open to the middle, check if the name is before or after, then repeat. Takes seconds.
The second approach is called Binary Search, and it's one of the most famous algorithms in computer science.
Simple Algorithm: Find the Biggest Number
javascript
// Algorithm: Find the largest number in a list
function findMax(numbers) {
let max = numbers[0]; // start with the first
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i]; // found a bigger one
}
}
return max;
}
console.log(findMax([3, 7, 2, 9, 4])); // 9Thinking Like a Programmer
When you face a problem, follow these steps:
1. Understand — what exactly is the problem asking?
2. Examples — try it with simple inputs by hand
3. Plan — write the steps in plain English before coding
4. Code — translate your plan into code
5. Test — try different inputs, including edge cases
6. Optimize — can you make it faster or simpler?
Pro Tip
Before writing a single line of code, solve the problem on paper first. If you can't explain the steps in plain English, you're not ready to code it yet.
Try It Yourself
Write an algorithm (in any language) that takes an array of numbers and returns true if the array contains any duplicates, false if all numbers are unique. Think about the steps before you code.
Ready to build?
Put what you learned into practice — pick a project and start coding.
Start Building Free