Table of Contents

    Writing Algorithms

    Writing Algorithms
    Figure: Writing Algorithms

    ALGORITHM FUNDAMENTALS

    Writing Algorithms

    An algorithm is a step-by-step procedure to solve a problem. Writing a good algorithm is the first step towards writing a successful program. Algorithms can be written in three main ways — Natural Language, Pseudocode, and Flowchart — each offering a unique way to represent your logic.

    Introduction

    Before you write a computer program, you need to plan how the program will work. This plan is called an algorithm. Writing an algorithm is like drawing a blueprint before building a house — it helps you organize your thoughts, define the logic, and avoid mistakes.

    There are three popular ways to write algorithms: Natural Language, Pseudocode, and Flowchart. Each has its strengths and is used based on the audience and complexity of the problem.

    Key Idea: A good algorithm is clear, correct, efficient, and easy to understand. It is the first step towards writing a successful program.

    Real-Life Analogy

    Writing an algorithm is like drawing a floor plan for a house. Before construction begins, the architect sketches every room, door, and window. Similarly, before coding, you write an algorithm to plan every step of your program.

    What is an Algorithm?

    An algorithm is a finite sequence of well-defined instructions used to solve a problem. It takes some input, processes it, and produces the required output.

    1

    Well-Defined Instructions

    Clear and precise steps

    Every step in an algorithm must be clear, unambiguous, and actionable.

    2

    Finite Steps

    Terminates in a limited number of steps

    A good algorithm must complete in a finite number of steps — never run forever.

    3

    Solves a Problem

    Takes input and gives output

    Every algorithm has a purpose: to solve a problem by taking input and producing the required output.

    Why Write Algorithms?

    Writing an algorithm before coding brings many benefits. Let's explore the main reasons why algorithms are essential.

    Organizes Thoughts

    • Helps in organizing thoughts
    • Breaks complex problems into small steps
    • Provides a clear plan

    Easy to Understand

    • Makes the solution easy to understand
    • Helps others follow your logic
    • Improves teamwork

    Correct & Efficient Programs

    • Helps in writing correct and efficient programs
    • Reduces bugs
    • Improves performance

    Blueprint Before Coding

    • Acts as a blueprint before coding
    • Saves time during implementation
    • Reduces trial-and-error

    Ways to Write Algorithms

    Algorithms can be written in three main ways, each suited for different audiences and purposes.

    A

    Natural Language

    Simple English descriptions

    Writing the algorithm in simple English like a normal conversation. Easy for beginners and non-programmers to understand.

    B

    Pseudocode

    English + Programming style

    Writing the algorithm using simple English statements with programming style. Bridges natural language and code.

    C

    Flowchart

    Visual diagram

    Representing the algorithm using symbols and arrows. A visual diagram that makes logic easy to see and follow.

    A. Natural Language

    The Natural Language approach uses simple, everyday English to describe the steps of an algorithm. It's the easiest way to start and requires no special knowledge.

    Advantages

    Pros
    • Easy to write and read
    • No special notation required
    • Understandable by anyone
    • Perfect for beginners

    Disadvantages

    Cons
    • Can be ambiguous or unclear
    • Not standardized
    • Harder to translate to code
    • Verbose for complex problems

    Example — Find Largest of Two Numbers (Natural Language)

    1. Start
    2. Take two numbers A and B
    3. If A > B then A is largest
    4. Else B is largest
    5. Display the largest number
    6. Stop
    Best For Beginners, teaching, brainstorming, and explaining logic to non-programmers.

    B. Pseudocode

    Pseudocode combines simple English with programming style. It uses keywords like START, INPUT, IF, THEN, ELSE, PRINT, and STOP but doesn't follow strict syntax of any programming language.

    Advantages

    Pros
    • Bridges natural language and code
    • Standardized keywords
    • Easy to translate to any programming language
    • Removes ambiguity
    • Language-independent

    Common Pseudocode Keywords

    Keyword Purpose
    START / BEGIN Start of the algorithm
    STOP / END End of the algorithm
    INPUT / READ Take input from user
    PRINT / OUTPUT / DISPLAY Display result
    IF...THEN...ELSE Decision making
    WHILE / FOR / REPEAT Looping
    ← or = Assignment operator
    ENDIF / ENDWHILE / ENDFOR End of block

    Example — Find Largest of Two Numbers (Pseudocode)

    START
    INPUT A, B
    IF A > B THEN
        LARGEST ← A
    ELSE
        LARGEST ← B
    ENDIF
    PRINT LARGEST
    STOP
    Best For Programmers, technical documentation, algorithm design, and interviews.

    C. Flowchart

    A Flowchart represents an algorithm using standard symbols and arrows. It's a visual way to see the flow of logic, making it easier to understand complex decisions and loops.

    Common Flowchart Symbols

    Symbol Name Purpose
    Oval Terminator Start or Stop of algorithm
    Parallelogram Input/Output Input data or output result
    Rectangle Process Perform a calculation or step
    Diamond Decision Ask a Yes/No question
    Arrow Flow Line Shows direction of flow
    Circle Connector Connects different parts

    Example — Find Largest of Two Numbers (Flowchart)

    Start (Oval)
       ↓
    Input A, B (Parallelogram)
       ↓
    Is A > B? (Diamond)
       ├── Yes → LARGEST = A (Rectangle)
       └── No  → LARGEST = B (Rectangle)
       ↓
    Print LARGEST (Parallelogram)
       ↓
    Stop (Oval)
    Best For Visual thinkers, complex logic, presentations, teaching, and documentation.

    Steps to Write an Algorithm

    Follow these 8 essential steps every time you write an algorithm.

    Step 1: Understand the Problem

    1

    Understand the Problem

    Read carefully

    Read the problem carefully and understand what is being asked.

    Example Find the sum of first N natural numbers.

    Step 2: Analyze the Problem

    2

    Analyze the Problem

    Identify what's needed

    Identify what the problem requires and how it can be solved.

    Example We need to add numbers from 1 to N.

    Step 3: Identify Inputs and Outputs

    3

    Identify Inputs and Outputs

    Decide the data

    Decide what data is needed as input and what result is expected as output.

    Example Input: N | Output: Sum

    Step 4: Develop the Logic

    4

    Develop the Logic

    Step-by-step plan

    Create step-by-step logic to solve the problem.

    Example Add 1 + 2 + 3 + ... + N and store the result in SUM.

    Step 5: Represent the Algorithm

    5

    Represent the Algorithm

    Choose the format

    Write the algorithm using natural language, pseudocode, or draw a flowchart.

    Step 6: Test the Algorithm

    6

    Test the Algorithm

    Try sample inputs

    Test the algorithm with sample data to ensure it works for all cases.

    Example Test with N = 5 → SUM = 15; N = 1 → SUM = 1; N = 10 → SUM = 55.

    Step 7: Refine and Debug

    7

    Refine and Debug

    Fix errors

    Check for errors and improve the algorithm if needed.

    Example Fix logic errors or handle edge cases like N = 0.

    Step 8: Implement (Coding)

    8

    Implement (Coding)

    Convert to code

    Convert the algorithm into a program using any programming language.

    Example Write code in C, Python, Java, or any other language.

    Example — Find Largest of Two Numbers

    Let's compare a good algorithm with a bad one for the same problem.

    Good Algorithm (Clear & Correct) ✓

    1. Start
    2. Input two numbers A and B
    3. If A > B then L = A
       Else L = B
    4. Print L
    5. Stop

    Why It's Good: Clear, complete, correct, and terminates.

    Bad Algorithm (Vague & Confusing) ✗

    1. Start
    2. Take two numbers
    3. Compare somehow
    4. Do some processing
    5. Display the largest number
    6. Go to step 3
    7. Stop (maybe)

    Why It's Bad: Vague, infinite loop, unclear termination.

    Characteristics of a Good Algorithm

    A well-written algorithm satisfies these 9 essential characteristics.

    # Characteristic Focus Goal
    1 Input Accept data Valid input
    2 Output Produce result Useful output
    3 Definiteness Clear steps Unambiguous
    4 Finiteness End in finite steps Termination
    5 Effectiveness Simple operations Feasible
    6 Generality Work for all cases Reusable
    7 Correctness Accurate result Reliability
    8 Efficiency Less time & memory Optimization
    9 Simplicity Easy to understand Clarity

    More Complete Examples

    Example 1: Check if a Number is Even or Odd

    Natural Language

    1. Start
    2. Take a number N
    3. If N is divisible by 2, then it's Even
    4. Otherwise, it's Odd
    5. Display the result
    6. Stop

    Pseudocode

    START
    INPUT N
    IF N MOD 2 == 0 THEN
        PRINT "Even"
    ELSE
        PRINT "Odd"
    ENDIF
    STOP

    Example 2: Sum of First N Natural Numbers

    Pseudocode

    START
    INPUT N
    SUM ← 0
    FOR i ← 1 TO N
        SUM ← SUM + i
    ENDFOR
    PRINT SUM
    STOP

    Example 3: Calculate Factorial of N

    Pseudocode

    START
    INPUT N
    FACT ← 1
    FOR i ← 1 TO N
        FACT ← FACT * i
    ENDFOR
    PRINT FACT
    STOP

    Example 4: Find Area of a Rectangle

    Pseudocode

    START
    INPUT L, B
    AREA ← L * B
    PRINT AREA
    STOP

    Comparison of Writing Methods

    Aspect Natural Language Pseudocode Flowchart
    Format Plain English English + keywords Visual diagram
    Ease of Writing Very Easy Moderate Slower
    Precision Can be ambiguous Precise Very precise
    Best For Beginners, non-tech users Programmers Complex logic
    Visual No No Yes
    Convertible to Code Difficult Easy Medium

    Did You Know?

    Interesting Fact

    The word "Algorithm" comes from the name of the great mathematician Muhammad ibn Musa Al-Khwarizmi, who lived in the 9th century! His work on Indian numerals and calculation methods laid the foundation for modern computing.

    Tips for Writing Better Algorithms

    Best Practices

    • Start by understanding the problem completely.
    • Use clear and precise language.
    • Number your steps for clarity.
    • Include Start and Stop at the beginning and end.
    • Handle edge cases (empty input, negative numbers).
    • Use simple, everyday words in natural language.
    • Choose the right format for your audience.
    • Test with different inputs before coding.
    • Keep it simple and readable.
    • Refine based on feedback.
    • Use tools like Draw.io for flowcharts.
    • Practice with common problems (sum, factorial, prime, sorting).

    Common Mistakes to Avoid

    Mistake 1: Vague Steps

    • "Compare somehow"
    • "Do processing"
    • Violates Definiteness

    Mistake 2: No Termination

    • Missing Stop step
    • Infinite loops
    • Violates Finiteness

    Mistake 3: Missing Input/Output

    • Not specifying inputs
    • No clear output
    • Incomplete algorithm

    Mistake 4: Skipping Testing

    • Not verifying with sample data
    • Bugs remain undetected
    • Fails at implementation

    Mistake 5: Overcomplicating

    • Too many nested conditions
    • Complex when simple works
    • Violates Simplicity

    Mistake 6: Wrong Format

    • Using flowchart for simple logic
    • Using natural language for complex
    • Choose based on need

    Frequently Asked Questions

    Q1. What are the 3 ways to write algorithms?

    The 3 main ways are: Natural Language, Pseudocode, and Flowchart.

    Q2. Which is the best way to write algorithms?

    It depends on the audience. Use natural language for beginners, pseudocode for programmers, and flowcharts for complex visual logic.

    Q3. What is pseudocode?

    Pseudocode is a way to write algorithms using English-like statements with programming keywords like IF, THEN, ELSE, PRINT — without being tied to any specific programming language.

    Q4. Is pseudocode a programming language?

    No! Pseudocode is NOT a programming language. It cannot be executed. It's just a way to describe algorithms clearly.

    Q5. When should I use a flowchart?

    Use flowcharts for algorithms with complex decisions, loops, and multiple paths where visual representation helps understanding.

    Q6. Can I write the same algorithm in all three ways?

    Yes! The same algorithm can be represented in natural language, pseudocode, or flowchart. Each is a different way to express the same logic.

    Q7. Do I need to write an algorithm before coding?

    Highly recommended! Writing an algorithm helps you plan, avoid mistakes, and produce cleaner, more efficient code.

    Q8. What tools can I use to draw flowcharts?

    Popular tools include Draw.io, Lucidchart, Microsoft Visio, Canva, and even PowerPoint.

    Q9. How long should an algorithm be?

    An algorithm should be as long as needed to clearly express the logic — no more, no less. Focus on clarity, not length.

    Q10. Can algorithms have loops and conditions?

    Absolutely! Loops (FOR, WHILE) and conditions (IF, ELSE) are common constructs used in algorithms to handle repetition and decision-making.

    Key Takeaways

    • An algorithm is a step-by-step procedure to solve a problem.
    • 3 ways to write: Natural Language, Pseudocode, Flowchart.
    • Natural Language uses simple English.
    • Pseudocode combines English with programming keywords.
    • Flowcharts use symbols and arrows for visual logic.
    • Follow 8 steps for systematic algorithm development.
    • A good algorithm has 9 essential characteristics.
    • Test with multiple inputs before coding.
    • Choose the writing method based on audience and complexity.
    • Practice regularly to improve your skills.

    Key Takeaway

    A good algorithm is clear, correct, efficient, and easy to understand. It is the first step towards writing a successful program. Master the three writing methods — Natural Language, Pseudocode, and Flowchart — and you'll be equipped to tackle any programming challenge.

    UNDERSTAND THE ALGORITHM, MASTER THE PROGRAM!

    Best of Luck! Practice more examples, think logically, code confidently. You can do it!

    Flowchart → Visual Thinking → Smart Solutions → Better Results! 🚀