Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

  1. Clear and Unambiguous – Each step is well-defined.
  2. Input – Takes zero or more inputs.
  3. Output – Produces at least one output.
  4. Finiteness – Completes after a finite number of steps.
  5. Effectiveness – Each step is basic enough to be performed exactly and in a reasonable time.

Example (Real-World)

Making Tea Algorithm:

  1. Boil water.
  2. Add tea leaves.
  3. Add sugar and milk.
  4. 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

  1. Start
  2. Input the first number.
  3. Input the second number.
  4. Add the two numbers.
  5. Output the result.
  6. 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

SymbolNamePurpose
🔵 OvalStart / EndIndicates the start or end of the process
RectangleProcessShows a task, step, or action to be performed
🔷 DiamondDecisionRepresents a question or condition (Yes/No)
ParallelogramInput / OutputUsed for reading inputs or displaying outputs
ArrowFlow LineShows the direction of process flow
🗂 Document ShapePredefined ProcessIndicates a subroutine or pre-defined process

flowchart

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.


Next: What is Pseudocode and Flowchart?