Topics
- 1. Definition of Algorithm
- 2. Characteristics (Properties) of an Algorithm
- 3. Algorithm and Program Notes
- 4. Definition of Pseudocode
- 5. Comparison: Algorithm vs Pseudocode
- 6. Steps to Design an Algorithm
An algorithm is a step-by-step procedure to solve a problem. It is a sequence of instructions that operates on input data to produce a desired output in a finite number of steps.
note : An algorithm is independent of any programming language.
Flow from Problem to Program
Problem
↓
Algorithm
↓
Data Structure
↓
Program
↓
Computer executes program
-
Definiteness / Clarity
-
Each step of an algorithm must be clear, simple, and unambiguous.
-
Every instruction should have only one interpretation.
-
-
Input
-
An algorithm must receive zero or more inputs supplied externally.
-
Example: Numbers to add, array elements to sort, etc.
-
-
Output
-
An algorithm must produce at least one result as output.
-
Example: Sum of numbers, sorted array, etc.
-
-
Finiteness
-
An algorithm must terminate after a finite number of steps.
-
It should not run indefinitely.
-
-
Effectiveness
-
Instructions of an algorithm must be simple enough to be carried out manually using pen and paper.
-
No step should require extraordinary intelligence to perform.
-
Example: Multiplying Two Numbers
1. Problem
Multiply two numbers and display the result.
2. Algorithm
Step-by-step solution in simple language:
-
Step 1: Store two inputs,
aandb. -
Step 2: Multiply
aandb, store the result inc. -
Step 3: Output
c.
Note: Algorithm is written in natural language, easy to understand, independent of programming language.
3. Program
Implementation of the algorithm in C language:
int main() {
int a, b, c;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
c = a * b;
printf("The result is %d\n", c);
return 0;
}
Note: A program is written in a computer language, executed by a computer, and depends on hardware, operating system, and compiler.
Comparison: Algorithm vs Program
| Aspect | Algorithm | Program |
|---|---|---|
| Definition | Step-by-step instructions to solve a problem | Implementation of an algorithm using a programming language |
| Language | Written in natural language | Written in a computer language |
| Dependency | Independent of OS and hardware | Depends on OS, hardware, and compiler |
| Execution | Conceptual – can be executed on paper | Executed by a computer |
| Phase in SDLC | Comes in Design/Planning Phase | Comes in Implementation Phase |
| Steps | Clear and unambiguous | Detailed instructions extended for the computer |
| Testing | No testing required on paper | Needs compilation, debugging, and testing |
-
Pseudocode is a method of writing an algorithm step by step.
-
It is a high-level description of an algorithm that combines:
-
Natural language (English-like instructions)
-
Mathematical logic
-
Key Point: Pseudocode is not a programming language and does not require execution.
Characteristics of Pseudocode
-
Step-by-step instructions to solve any problem.
-
Does not require specifying input and output explicitly.
-
Written in natural language, sometimes mixed with simple mathematical symbols.
-
No specific syntax rules.
-
Does not raise errors because it is not executed.
-
High-level description of an algorithm.
| Aspect | Algorithm | Pseudocode |
|---|---|---|
| Definition | Step-by-step instructions to solve a problem | High-level description of an algorithm |
| Language | Natural language | Natural language + basic mathematical logic |
| Input/Output | Must define input and output | Not strictly required |
| Execution | Conceptual, can be executed manually | Cannot be executed; purely descriptive |
| Syntax | No strict syntax rules | No specific syntax rules |
| Purpose | To design a solution conceptually | To make algorithm easier to implement in code |
Pseudocode Example
Problem: Find the greatest of two numbers
Input: a, b
If a > b then
Print "a is greatest"
Else
Print "b is greatest"
End