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.
Watch these quick videos to learn what algorithms are and how they work:
https://www.youtube.com/watch?v=6hfOvs8pY1k
https://www.youtube.com/watch?v=ZnBF2GeAKbo
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;
}
<aside>
💡 Algorithms are not about code, they’re about the logic behind the code.
</aside>
Before learning specific algorithms like sorting and searching, it’s important to understand why they exist. As problems get bigger and data grows, simple logic isn’t always enough. Different algorithms solve the same problem in different ways, and some ways are much faster or more efficient than others.
In the next sections, you’ll see that algorithms are simply patterns of problem-solving. The goal is not to memorize them, but to understand how they work, when to use them, and how to recognize similar patterns in new problems. Once you understand the idea behind an algorithm, you can apply it in any programming language.
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
<aside>
⚠️ Linear search becomes slow for very large datasets, but it is perfect for beginners and everyday coding tasks.
</aside>
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]
<aside>
⚠️ Bubble Sort is not fast for large inputs — but it is excellent for understanding how sorting works.
</aside>
<aside>
💡 Bubble Sort helps understand loop logic, comparisons, swaps, and iteration patterns.
</aside>
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]
<aside>
</aside>
Even though JavaScript has .sort(), learning these fundamentals helps you:
This video gives you a visual tour of different sorting algorithms, all shown side-by-side in action. Instead of just looking at code, you see how data moves and changes with each step, which makes the concepts much easier to understand.