What Is an Algorithm?
An algorithm is a step-by-step procedure used to solve a particular problem. It takes some input, performs a finite sequence of well-defined steps, and produces a definite output.
Example: Find the sum of two numbers.
Step 1: Start
Step 2: Read A and B
Step 3: Sum = A + B
Step 4: Print Sum
Step 5: Stop
Characteristics (Properties) of an Algorithm
Every good algorithm must satisfy the following five essential properties.
1. Input
An algorithm should accept zero or more inputs. These inputs are the values provided to the algorithm to solve a particular problem.
Example: The sum-of-two-numbers algorithm accepts two inputs, A and B. An algorithm that displays a fixed message such as "Hello" does not require any input. Therefore, zero inputs are also allowed.
2. Output
An algorithm should produce at least one output. The output represents the result obtained after processing the inputs.
Example: The sum-of-two-numbers algorithm produces the sum of A and B as its output. If A = 10 and B = 20, the output is 30.
3. Definiteness
Every instruction in an algorithm must be clear and unambiguous. Each step should have a precise meaning so that it can be understood and executed without confusion.
Example: Consider calculating the average of two numbers.
Unclear instruction: Calculate the average properly.
This instruction is ambiguous because it does not specify how the average should be calculated.
Clear instruction: Add the two numbers and divide their sum by 2.
The second instruction clearly explains the required calculation and therefore satisfies the property of definiteness.
4. Finiteness
An algorithm must terminate after a finite number of steps. It should not continue executing forever.
Example (finite):
A loop that runs from i = 1 to 10 and then stops.
It has a clear stopping point, so it satisfies finiteness.
Example (not finite):
while (true) { ... } with no stopping condition.
It continues forever, so it violates the finiteness property.
5. Effectiveness
Every instruction in an algorithm must be basic, practical, and executable in a finite amount of time. Each operation should be sufficiently simple to perform using the available operations.
Example (not effective):
"Sort the list."
This does not specify the basic operations needed to perform the sorting.
Example (effective):
"Compare two adjacent elements. If the first is greater, swap them."
This describes a clear, executable operation.
Quick Revision: Five Properties of an Algorithm
- Input: Accepts zero or more inputs.
- Output: Produces at least one output.
- Definiteness: Every instruction is clear and unambiguous.
- Finiteness: Terminates after a finite number of steps.
- Effectiveness: Every instruction is basic and executable.
An algorithm must satisfy all five properties to be considered as a proper algorithm.