What Is an Algorithm? Meaning, Types, and Examples
An algorithm is a finite list of clear steps that turns an input into an output. Every step must have one meaning, and the list must stop after a limited number of steps.
A recipe is the everyday version of the idea. It names the ingredients, gives ordered steps, and ends with a dish.
In computer science the word is stricter. The method must work for every valid input, not for one lucky case.
A worked example
Here is an algorithm that finds the largest number in a list.
- Set
bestto the first number. - Read each remaining number in order.
- If that number is larger than
best, setbestto it. - When the list ends, return
best.
Give it the list 4, 9, 2. It sets best to 4, then to 9, then keeps 9, then returns 9.
The method reads each item once. For a list of n items it does about n comparisons.
The five properties of an algorithm
Textbooks list five properties. A method that misses any one of them is not an algorithm.
| Property | What it means |
|---|---|
| Input | It takes zero or more defined inputs. |
| Output | It produces at least one result. |
| Definiteness | Every step is exact and has one meaning. |
| Finiteness | It stops after a limited number of steps. |
| Effectiveness | Each step is simple enough to carry out by hand. |
A loop with no exit fails finiteness. A step that says "sort it somehow" fails definiteness.
The four main types of algorithms
Algorithms are grouped by the design method behind them. Four methods cover most problems you will meet in study and in interviews.
| Type | Core idea | Examples |
|---|---|---|
| Brute force | Try every candidate answer. | Linear search, bubble sort |
| Divide and conquer | Split the problem, solve each part, then join the parts. | Merge sort, binary search |
| Greedy | Take the best choice available right now. | Huffman coding, activity selection |
| Dynamic programming | Solve each smaller case once, store it, reuse it. | Knapsack, longest common subsequence |
Brute force is the simplest and the slowest. It is still useful as a first working answer and as a check on a faster one.
Dynamic programming applies when the same smaller case appears again and again. Storing each answer once removes the repeated work.
Divide and conquer vs greedy
These two get mixed up often, so here is the split.
Divide and conquer breaks a problem into smaller copies of the same problem. It solves each copy and then combines the results.
Greedy never makes copies. It builds one answer, picks the best option at each step, and never changes a past choice.
| Question | Divide and conquer | Greedy |
|---|---|---|
| Subproblems | Smaller copies of the same problem | None |
| Combine step | Yes, the parts are merged | No |
| Revisits a choice | Not needed | Never |
| Always correct | Yes, when the split and merge are correct | Only when the greedy choice is provably safe |
| Common cost | O(n log n), as in merge sort | O(n log n), mostly from sorting the input first |
Greedy is shorter to write and often faster to run. It is also wrong on many problems.
Use it only when you can argue that the best local choice cannot block a better final answer.
Where machine learning algorithms fit
A machine learning algorithm uses the word in a wider sense. It is still a step-by-step method, but the steps adjust numbers inside a model.
Sorting gives the same output for the same input every time. Training gives a model whose output depends on the data it saw.
Linear regression, decision trees, k-means and gradient descent are the common names. They belong to a separate branch of the field.
How an algorithm is measured
Two costs matter: time and memory.
Time is counted in basic operations, not in seconds, because seconds depend on the machine. Memory is counted as extra storage beyond the input itself.
Both are written with Big O notation, which reports how the cost grows as the input grows.
| Notation | Steps for 1,000 items | Typical source |
|---|---|---|
| O(1) | 1 | A hash map lookup |
| O(log n) | about 10 | Binary search |
| O(n) | 1,000 | One pass over a list |
| O(n log n) | about 10,000 | Merge sort |
| O(n squared) | 1,000,000 | Two nested loops |
The gap between the last two rows is the reason sorting beats nested loops on large inputs.
How to Prepare
- Learn the four methods, not hundreds of problems. Most questions are one of these methods applied to a new setting. Grokking the Coding Interview groups problems by the pattern they share.
- State the cost before you code. Say the time and space in Big O, then write the solution. Interviewers score that habit.
- Drill the two hard methods. Greedy and dynamic programming cause most failures. Grokking Data Structures for Coding Interviews covers the structures both of them need.
- Explain out loud. A mock interview shows whether your reasoning is clear to another person.
- Read the short follow-ups. See tips for acing algorithm design interviews and visual mnemonics for algorithm patterns.

GET YOUR FREE
Coding Questions Catalog

$123

$197

$72