Algorithms are simply step-by-step instructions for solving a problem. Every time you write a function, design a feature, or debug an issue, you’re already thinking algorithmically.
In this lesson, we explore two essential algorithm patterns:
These are not about writing the fastest possible code — they are about thinking like a developer and understanding the building blocks of problem-solving.
An algorithm is a clear, repeatable process that solves a problem.
You already use algorithms in daily life:
// A simple algorithm: find the smallest number in an array
function findMin(numbers) {
let min = numbers[0];
for (const num of numbers) {
if (num < min) {
min = num;
}
}
return min;
}
💡 Algorithms are not about code — they’re about the logic behind the code.
Searching means finding an item inside a list. The easiest and most common beginner technique is Linear Search.
Linear search checks each item one-by-one until it finds the target.
Imagine searching for a book on a messy shelf:
You start from the left and check each book until you find the one you want.
FOR each item in the list
IF item equals the target
RETURN the index
RETURN -1 (not found)
function linearSearch(array, target) {
for (let i = 0; i < array.length; i++) {
if (array[i] === target) {
return i;
}
}
return -1;
}
console.log(linearSearch([10, 20, 30], 20)); // 1
console.log(linearSearch([10, 20, 30], 50)); // -1
⚠️ Linear search becomes slow for very large datasets — but it is perfect for beginners and everyday coding tasks.
Sorting means arranging items in a list into a specific order (e.g., smallest to largest).
There are many advanced sorting algorithms, but beginners start with:
We focus on understanding, not memorizing.
Bubble Sort compares pairs of neighbors and swaps them if they’re in the wrong order.
It "bubbles" the biggest number to the end of the list with each pass.
Imagine sorting numbers by repeatedly checking pairs:
[5, 3, 8, 2]
Compare 5 and 3 → swap → [3, 5, 8, 2]
Compare 5 and 8 → ok
Compare 8 and 2 → swap → [3, 5, 2, 8]
REPEAT until no swaps are needed:
SET swapped to false
FOR each pair of neighbors in the array:
IF left > right:
SWAP them
SET swapped to true
function bubbleSort(arr) {
let swapped = true;
while (swapped) {
swapped = false;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
const temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
}
return arr;
}
console.log(bubbleSort([5, 3, 8, 2])); // [2, 3, 5, 8]
⚠️ Bubble Sort is not fast for large inputs — but it is excellent for teaching how sorting works.
💡 Bubble Sort helps students understand loop logic, comparisons, swaps, and iteration patterns.
Selection Sort finds the smallest item and moves it to the beginning.
Then repeats for the rest of the array.
[5, 3, 8, 2]
Smallest is 2 → swap with first → [2, 3, 8, 5]
Next smallest is 3 → already in place
Next smallest is 5 → swap with 8 → [2, 3, 5, 8]
FOR each position in the array:
FIND the smallest item from the unsorted part
SWAP it with the item in the current position
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap
const temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
console.log(selectionSort([5, 3, 8, 2])); // [2, 3, 5, 8]
💡 Selection Sort teaches pattern recognition — a key developer skill.
Even though JavaScript has .sort(), learning these fundamentals helps you: