Algorithm
In one line
An algorithm is a finite sequence of well-defined steps that solve a specific problem or perform a task.
In simple words
An algorithm is a recipe for solving a problem. It is a precise, step-by-step procedure that takes an input, processes it through a series of defined steps, and produces an output. Every program implements one or more algorithms.
Common algorithms include sorting (bubble sort, quicksort, merge sort), searching (linear search, binary search), and pathfinding (Dijkstra, A*). Each algorithm has trade-offs: some are faster but use more memory, some are simple but slow, some work well on small data but poorly on large data.
Algorithm efficiency is measured in Big O notation — how the runtime grows as the input size grows. O(1) is constant time, O(n) is linear, O(n²) is quadratic. Understanding algorithms and their efficiency is a core programming skill.
Example
// Linear search algorithm
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1; // not found
}
console.log(linearSearch([3, 1, 4, 1, 5], 4)); // 2Linear search checks each element one by one until it finds the target. It is O(n) — in the worst case, it checks every element.
Related terms
Related Resources
Related Lessons
- Algorithm Thinking
Learn about algorithms.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.