What is an Algorithm?
An algorithm is a step-by-step procedure or a set of instructions designed to perform a specific task or solve a problem.
It is like a recipe in cooking — a list of steps you follow in a specific order to achieve the desired outcome.
Characteristics of a Good Algorithm
- Clear and Unambiguous – Each step is well-defined.
- Input – Takes zero or more inputs.
- Output – Produces at least one output.
- Finiteness – Completes after a finite number of steps.
- Effectiveness – Each step is basic enough to be performed exactly and in a reasonable time.
Example (Real-World)
Making Tea Algorithm:
- Boil water.
- Add tea leaves.
- Add sugar and milk.
- Pour into a cup and serve.
Here, each step is clear, ordered, and finite — just like in programming.
Example of an Algorithm – Adding Two Numbers
Let’s take a very simple problem:
Problem:
Write an algorithm to add two numbers and display the result.
Step-by-Step Algorithm
- Start
- Input the first number.
- Input the second number.
- Add the two numbers.
- Output the result.
- End
Flowchart Representation
Flowcharts are visual diagrams that show the steps of an algorithm using shapes and arrows.
Benefits:
- Easy to understand for beginners.
- Shows the flow of logic visually.
- Useful for planning before coding.
Common Flowchart Symbols
| Symbol | Name | Purpose |
|---|---|---|
| 🔵 Oval | Start / End | Indicates the start or end of the process |
| ▭ Rectangle | Process | Shows a task, step, or action to be performed |
| 🔷 Diamond | Decision | Represents a question or condition (Yes/No) |
| ▱ Parallelogram | Input / Output | Used for reading inputs or displaying outputs |
| ➡ Arrow | Flow Line | Shows the direction of process flow |
| 🗂 Document Shape | Predefined Process | Indicates a subroutine or pre-defined process |
Explanation of the Flowchart
- Start (A): Represented by an oval, indicating the beginning of the algorithm.
- Input first number (B): A parallelogram, showing the user inputs the first number.
- Input second number (C): Another parallelogram, for inputting the second number.
- Add numbers (D): A rectangle, representing the processing step where the two numbers are added.
- Display sum (E): A parallelogram, indicating the output of the sum to the user.
- End (F): An oval, marking the end of the algorithm.
- Arrows: Show the flow of execution from one step to the next.
import java.util.Scanner;
public class AddTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
// Processing
int sum = num1 + num2;
// Output
System.out.println("Sum = " + sum);
sc.close();
}
}
🔍 Examples of Algorithms in Computer Science
- Searching: Linear Search, Binary Search
- Sorting: Bubble Sort, Merge Sort
- Graph Traversal: BFS, DFS
- Dynamic Programming: Fibonacci, Knapsack
💡 Key Takeaway
An algorithm is not code — it’s the logic and steps to solve a problem.
You can write algorithms in plain language, pseudocode, or flowcharts before turning them into Java code.