What is an algorithm?
In short
An algorithm is a clear, step-by-step method for solving a problem. A program is an algorithm written in a language a computer can run.
A recipe is an algorithm: follow the steps in order and you get the same cake every time. In programming, algorithms do jobs like sorting a list, finding the shortest route or checking whether a word is a palindrome.
Learning algorithms is really learning to think in steps: what do I need to remember, what do I repeat, and when do I stop? The example below finds the biggest number by looking at each one in turn and remembering the biggest so far.
Python
Find the biggest number, one step at a time
numbers = [4, 17, 9, 23, 5]
biggest = numbers[0]
for n in numbers:
if n > biggest:
biggest = n
print(biggest)Output
23JavaScript
The same algorithm in JavaScript
const numbers = [4, 17, 9, 23, 5];
let biggest = numbers[0];
for (const n of numbers) {
if (n > biggest) {
biggest = n;
}
}
console.log(biggest);Output
23Learn it properly
These free lessons teach it step by step, with questions and a program you write yourself.
- Finding and CountingLearn Python →
- Looping and Searching ArraysLearn JavaScript →
- Running Totals and Nested LoopsLearn Python →
Try your first lesson free
Codiqo teaches coding in five-minute lessons with real code. Free on the web, iPhone and Android.