1. Introduction to Algorithms
An algorithm is a well-defined sequence of steps used to solve a specific problem. It
provides a clear procedure that a computer program can follow to achieve the desired
output from a given input.
In computer science, algorithms are fundamental because every program is essentially
an implementation of an algorithm. Efficient algorithms help in solving problems
faster and using fewer resources such as memory and processing time.
An algorithm must clearly define how the input is transformed into output through a
finite number of logical steps.
For example, a simple algorithm can be written to find the maximum value in a list of
numbers.
Example Algorithm (Find Maximum)
Start
Read array elements
Assume first element as maximum
Compare each element with maximum
If element > maximum, update maximum
Print maximum value
End
This algorithm solves the problem using a clear step-by-step procedure.
2. Algorithm Specification
Algorithm specification refers to the formal description of an algorithm, explaining
how the algorithm works, what inputs it requires, and what outputs it produces.
Algorithms are usually specified using:
Natural language
Pseudocode
Flowcharts
Mathematical notation
A good algorithm specification helps programmers easily implement the algorithm in
any programming language such as C++.
Example Pseudocode
, Algorithm FindMaximum(A, n)
1. max ← A[0]
2. for i ← 1 to n-1
3. if A[i] > max
4. max ← A[i]
5. print max
This pseudocode clearly describes the logic without depending on a specific
programming language.
3. Properties of an Algorithm
For a procedure to be considered a valid algorithm, it must satisfy several important
properties.
3.1 Input
An algorithm must accept zero or more inputs.
Inputs represent the data that the algorithm processes.
Example:
In a sorting algorithm, the input is a list of numbers.
Example input:
A = {7, 2, 5, 1, 9}
3.2 Output
An algorithm must produce at least one output.
The output is the result obtained after performing the algorithm.
Example:
For a sorting algorithm, the output would be:
{1, 2, 5, 7, 9}
3.3 Definiteness
Every step in an algorithm must be clearly defined and unambiguous.
There should be no confusion about what action should be performed.
Example of a bad instruction