Algorithm
An algorithm is a well-defined procedure that accepts input(s),
processes them, and produces an output.
It is like a recipe in cooking:
o Ingredients = Input
o Cooking steps = Processing steps
o Final dish = Output
Example:
To make tea →
1. Boil water
2. Add tea leaves
3. Add sugar & milk
4. Stir and serve
→ That is an algorithm for tea-making.
Need of Algorithms
Algorithms are needed in computer science and daily life for several
reasons:
1. Problem Solving
o Breaks big problems into smaller steps.
o Example: Sorting students’ marks → first collect marks, then
compare, then arrange.
2. Efficiency
o Helps to solve tasks quickly.
o Example: Searching a word in a dictionary → Binary Search
Algorithm (checks middle page first, not from the first page).
3. Automation
o Computers cannot think; they only follow algorithms.
o Example: ATM withdrawal → algorithm checks PIN, verifies
balance, then gives cash.
4. Data Processing
o Large data (like in Google search) needs systematic steps.
o Algorithms process, filter, and present the best results.
5. Optimization
o Finds the best way among many.
o Example: Google Maps uses shortest path algorithms to
suggest the fastest route.
6. Security
o Cryptographic algorithms secure passwords and online
payments.
o Example: RSA and AES algorithms for encryption.
7. Innovation
o AI, machine learning, and robotics all work on complex
algorithms.
Characteristics of Algorithms
1. Finite
o Must end after a limited number of steps.
o Example: Factorial of a number n will stop after multiplying all
numbers from n to 1.
2. Well-Defined
o Steps must be clear, no confusion.
o Example: “Add 5” is clear; “Do something with 5” is not.
3. Input/Output
o Takes input → produces output.
o Example: Input = 2, 3 → Output = 5 (for addition).
4. Deterministic
o Same input always gives same result.
o Example: √9 = 3 always, not sometimes 2.
5. Effective
o Steps must work correctly to solve the problem.
o Example: Algorithm for finding largest number must always
show correct largest.
6. Efficient
o Uses minimum time and memory.
o Example: Linear Search checks one by one → slow; Binary
Search → faster and efficient.
Qualities of a Good Algorithm
1. Correctness
o Gives right answer for all inputs.
o Example: If algorithm for division fails when divisor=0, it is not
correct.
2. Clarity & Understandability
o Easy to read and write, not confusing.
o Example: Step-wise pseudocode is clearer than long, complex
sentences.
3. Efficiency
o Saves time and space.
o Example: Merge Sort is more efficient than Bubble Sort for
large data.
4. Robustness
o Handles unexpected cases.
o Example: Calculator program must show error if someone
divides by zero.
5. Scalability
o Works even when problem size grows.
o Example: Google Search handles millions of queries, not just
one.
6. Modularity
o Can be broken into smaller modules (parts).
o Example: Online shopping app → Login module, Cart module,
Payment module.
7. Flexibility & Adaptability
o Can be applied to different inputs.
o Example: Sorting algorithm should work for numbers, marks,
names, etc.
8. Optimality
o Should give best solution, not just any solution.
o Example: Dijkstra’s Algorithm finds shortest path, not just any
path.
9. Simplicity
o Simple algorithms are easier to debug and maintain.
o Example: Swapping two numbers → simple 3-step process.
10. Well-Tested
Must be tested on different types of inputs (normal, large, invalid).
Example: Test login algorithm with correct, wrong, and empty
password.
Example 1: Adding Two Numbers
Input: num1, num2
Steps:
1. Start
2. Read num1, num2
3. sum = num1 + num2
4. Display sum
5. Stop
Output: sum
Example 2: Largest of Two Numbers
Input: num1, num2
Steps:
1. Start
2. Read num1, num2
3. If num1 > num2 → largest = num1
Else → largest = num2
4. Display largest
5. Stop
Output: largest number
An algorithm is a roadmap for solving problems.
It should be clear, finite, correct, efficient, scalable, and
robust.
Used everywhere: banking, healthcare, AI, internet, daily life.