Algorithm
- In Turkish
- Algoritma
In short
An algorithm is a finite, step-by-step set of instructions for solving a problem or completing a task, such as sorting a list or finding the shortest route.
What is an algorithm?
An algorithm is a precise recipe for turning an input into a desired output. It spells out every step clearly enough that anyone, or any computer, following it will get the same result. Algorithms exist independently of programming languages: the same algorithm can be written in Python, in JavaScript, or on paper.
A cooking recipe is a good everyday analogy for an algorithm. It lists ingredients (the input), gives ordered steps, and ends with a finished dish (the output). In software, familiar examples include binary search, which finds an item in a sorted list by repeatedly cutting the list in half, and the sorting algorithms behind functions like Array.prototype.sort.
Different algorithms can solve the same problem with very different efficiency. Developers compare them using Big O notation, which describes how running time or memory use grows as the input gets larger. For example, checking every item in a list one by one is O(n), while binary search is O(log n), which is far faster for large lists.
An algorithm is not the same thing as a program. The algorithm is the idea, the logical sequence of steps, while a program is a concrete implementation of one or more algorithms in a specific language, together with everything else needed to run it.
At a glance
Key takeaways
- An algorithm is a finite, ordered set of steps that turns an input into an output.
- It is language-independent; code is just one way to express it.
- Efficiency is described with Big O notation for time and memory.
- Classic examples include searching, sorting, and shortest-path algorithms.
Example
// Find target in a sorted array; return its index or -1
function binarySearch(sorted, target) {
let low = 0;
let high = sorted.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (sorted[mid] === target) return mid; // found it
if (sorted[mid] < target) low = mid + 1; // search the right half
else high = mid - 1; // search the left half
}
return -1; // not found
}
binarySearch([2, 5, 8, 12, 16], 12); // 3Readers ask
What is the difference between an algorithm and a program?
An algorithm is the abstract sequence of steps for solving a problem. A program is a concrete implementation of that algorithm, usually along with many others, written in a specific programming language so a computer can run it.
What is Big O notation?
Big O notation describes how an algorithm's running time or memory use grows as the input size grows. For example, O(n) means the work grows in direct proportion to the input, while O(1) means it stays the same no matter how large the input is.
Do I need to know algorithms to be a developer?
You don't need to memorize every algorithm, but understanding common ones like searching and sorting, and knowing how to reason about efficiency, helps you write faster code. Algorithm questions are also common in technical interviews.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- RecursionProgramming Fundamentals, p. 48Recursion is a technique in which a function solves a problem by calling itself on smaller versions of the same problem until it reaches a simple base case.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- Big O NotationProgramming Fundamentals, p. 6Big O notation describes how an algorithm's running time or memory use grows as its input gets larger, focusing on the growth rate rather than exact speed.
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
- Machine LearningAI & Machine Learning, p. 27Machine learning is a branch of artificial intelligence in which computers learn patterns from data to make predictions instead of following hand-written rules.
Spotted a mistake or something missing on this page?Suggest an edit