Algorithm
A step-by-step procedure for solving a problem or computing a result. Algorithms are the core of computer science — sorting, searching, graph traversal, etc. Measured by time complexity (how fast) and space complexity (how much memory), often in Big-O notation.
- Always — every program uses algorithms.
- Never.
Definition
An algorithm is a finite sequence of instructions that transforms input to output. Correctness means it produces the right answer for all valid inputs. Efficiency is measured in time (number of operations) and space (memory) as input grows, expressed in Big-O.
Why it exists
To solve problems systematically and efficiently. The right algorithm can turn an impossible (O(n!)) computation into a fast (O(n log n)) one.
How it works
Design the steps, prove correctness (handles edge cases, terminates), and analyze efficiency (Big-O). Common patterns: brute force, divide-and-conquer, greedy, dynamic programming, backtracking. Data structure choice affects algorithm efficiency.
Scaling limits
Some problems are inherently hard (NP-complete). Algorithm design is a skill that takes practice. Over-optimizing before measuring is a common mistake.
Cost shape
Free.
Alternatives
- Guess and check — No design, but slow and wrong.
Failure modes
- Wrong algorithm (doesn't handle edge cases).
- Inefficient algorithm (too slow).
- Premature optimization.