What Is an Algorithm? Steps, Correctness, and Efficiency Explained
Follow a simple search to see how a problem becomes a precise procedure.
Key takeaways
- An algorithm is a defined procedure for solving a problem or performing a computation.
- Inputs, assumptions, outputs, and stopping conditions determine what the procedure promises.
- Different algorithms can solve the same problem with different time and memory costs.
- Code implements a method, while a larger product may combine many algorithms and learned models.
An algorithm is a defined procedure for solving a problem or carrying out a computation. It specifies steps that can be followed, rather than merely naming the desired result.
“Find a product” is a goal. “Check each product identifier until one matches, or until the list ends” describes a method.
That method can be written in JavaScript, performed by a person on paper, or expressed as pseudocode. The implementation changes, but the underlying procedure can remain the same.
Turn the Goal Into a Precise Contract
Suppose an application needs to find a product ID in a list.
Before writing the procedure, decide what “find” means. Should it return the product, its position, or simply true or false? If the ID occurs twice, should it return the first occurrence? What happens when the list is empty?
For this example, the input is an array of numeric IDs and a numeric target. The output is the first matching index, or -1 if no match exists.
That contract gives the algorithm something specific to satisfy.
A Linear Search Checks Each Candidate
function findFirst(ids, target) {
for (let index = 0; index < ids.length; index += 1) {
if (ids[index] === target) {
return index;
}
}
return -1;
}
findFirst([14, 28, 35, 28], 28); // 1
findFirst([14, 28, 35], 99); // -1
The procedure begins at index zero, compares the current value, and advances only when it has not found a match.
It stops either by returning a matching index or by reaching the end. The empty array also has a defined outcome: the loop performs no comparisons and returns -1.
MDN’s algorithm definition describes the general idea of a specified sequence for completing a task. This search makes that idea concrete.
Correctness Depends on the Assumptions
The example compares numbers using strict equality. A string containing "28" is not the same input value as the number 28.
If IDs arrive from an external source, the application must establish their representation. An algorithm cannot silently repair an unclear input contract.
That is why parsing and runtime validation often happen before the main procedure.
Correctness means the algorithm meets its stated requirements for the inputs those requirements cover.
Another Algorithm Can Do Less Work
Linear search may inspect every item. Its worst-case number of comparisons grows in proportion to the list length, commonly described as O(n).
If an array is sorted, binary search can repeatedly discard half the remaining candidates. With efficient indexed access, it uses O(log n) comparisons. NIST’s binary-search entry explains that process.
The sorted-input requirement matters. Applying binary search to an unsorted list can return the wrong result.
Sorting a list solely for one search also has a cost. For repeated lookups, preparation may be worthwhile; for a tiny list searched once, the simpler scan may be sufficient.
Efficiency Includes More Than Running Time
An algorithm can trade memory for speed, do preparation before queries, or produce an approximate result instead of an exact one.
| Question | Why it matters |
|---|---|
| How large is the input? | A cost invisible on ten items can dominate on millions |
| How often is the operation repeated? | Preparation can pay off across many queries |
| Can the data change? | An index or sorted representation needs maintenance |
| How much memory is available? | Faster lookup structures can require extra storage |
| What result is acceptable? | Exact and approximate solutions may have different costs |
Big-O describes growth under a model. It does not provide a stopwatch reading or eliminate the need to understand the workload.
The Objective Shapes the Result
Consider a route planner. The shortest distance, fastest journey, and lowest toll cost can identify different routes.
A ranking system likewise needs a definition of what it should prefer. Maximizing predicted clicks is not identical to maximizing usefulness or accuracy.
An algorithm may perform exactly as specified while producing an undesirable result because the objective was poorly chosen.
This is especially relevant when a product’s many procedures are collectively described as “the algorithm.”
Algorithms and AI Are Not Synonyms
A linear search uses no machine learning. A machine-learning system, however, relies on algorithms to prepare data, train models, and use their outputs.
A learned model and the procedure that trained it are related but different things. The surrounding application also includes ordinary rules, storage, and communication.
When evaluating an algorithm, ask what it receives, what it promises, why its steps meet that promise, and how its costs grow. Those questions remain useful whether the procedure searches four IDs or forms one part of a large software system.
A procedure can follow its rules perfectly and still solve the wrong problem.
More Articles Like This
What Is Python? A Practical Introduction to the Language

Regex vs Parsing: Choosing the Right Tool for Structured Text
What Is a Regular Expression? Regex Patterns Explained With Examples
