Table of Contents

    Introduction to Algorithm

    Introduction to Algorithm
    Figure: Introduction to Algorithm

    PROGRAMMING FUNDAMENTALS

    Introduction to Algorithm

    An Algorithm is a finite, well-defined, and step-by-step set of instructions used to solve a problem or perform a specific task. Algorithms are the foundation of computer programming, data processing, mathematics, and even everyday problem-solving.

    What is an Algorithm?

    An Algorithm is a sequence of unambiguous steps that defines how a problem is solved. It provides a clear, logical, and step-by-step procedure to achieve a desired outcome from a given input.

    Algorithms are used in computer programming, mathematics, data processing, and even in everyday life — like making a cup of tea, following a recipe, or withdrawing money from an ATM.

    Key Idea: An algorithm is a step-by-step solution to a problem. It must be clear, finite, and produce a correct result every time.

    Real-Life Analogy

    A cooking recipe is an algorithm. It has a list of ingredients (input), a sequence of steps (process), and a delicious dish (output). Follow the steps correctly, and you always get the same great result.

    Characteristics of an Algorithm

    A good algorithm must satisfy five essential characteristics. These qualities ensure that the algorithm is correct, efficient, and understandable.

    1

    Finiteness

    Must have a finite number of steps

    An algorithm must end after a finite number of steps. It should never run forever or continue indefinitely.

    2

    Definiteness

    Each step must be clear and precise

    Each step of an algorithm should be unambiguous and clearly defined. There should be no room for confusion or multiple interpretations.

    3

    Input

    Accepts zero or more inputs

    An algorithm may take zero or more input values. Inputs are the data the algorithm works on to produce the desired output.

    4

    Output

    Produces at least one output

    Every algorithm must produce at least one output. The output is the solution or result of the problem being solved.

    5

    Effectiveness

    Each step must be basic and feasible

    Every step of the algorithm must be simple enough to be executed and must be achievable in a reasonable amount of time.

    Importance of Algorithms

    Algorithms are the heart of computer science and programming. They play a critical role in software development, artificial intelligence, and every digital application we use.

    Why Algorithms Matter

    • Provide a logical approach to solving problems
    • Help in designing efficient programs
    • Improve code readability and maintainability
    • Save time and system resources
    • Essential in programming, data structures, and AI

    Where Algorithms Are Used

    • Search engines (Google, Bing)
    • Social media feeds (Facebook, Instagram)
    • Navigation apps (Google Maps)
    • Online shopping recommendations
    • Banking, healthcare, and cybersecurity

    How an Algorithm Works

    An algorithm works on a simple flow: it takes some input, processes it through a series of well-defined steps, and produces an output.

    ALGORITHM FLOW
    Input (Data)Process (Steps / Instructions)Output (Result)

    Algorithms take input, process it using a series of steps, and produce the desired output. Every computer program, calculator function, or intelligent system works on this basic principle.

    Example — Algorithm to Find the Largest Number

    Let's look at a classic example of an algorithm that finds the largest of three numbers.

    Problem Statement

    Given three numbers A, B, and C, find the largest number among them.

    Algorithm Steps

    Step 1: Start
    Step 2: Read three numbers: A, B, C
    Step 3: If A ≥ B and A ≥ C, then A is largest
    Step 4: Else if B ≥ A and B ≥ C, then B is largest
    Step 5: Else C is largest
    Step 6: Display the largest number
    Step 7: Stop
    Example If A = 10, B = 25, C = 15 → The largest number is B = 25.

    Types of Algorithms

    Algorithms are classified into different types based on their logic and flow of control. Let's explore the four main types.

    1

    Sequential Algorithm

    Steps executed one after another

    In a Sequential Algorithm, steps are performed in a straight line, one after the other, without any branching or looping.

    Example Turn on computer → Login → Open browser → Search Google → Read result.
    2

    Conditional Algorithm

    Based on conditions, different paths are followed

    A Conditional Algorithm uses if-else statements to make decisions and choose different paths based on the input.

    Example If temperature > 30°C → turn on AC else → turn on fan.
    3

    Iterative (Loop) Algorithm

    Steps are repeated multiple times until a condition is met

    An Iterative Algorithm uses loops (for, while, do-while) to repeat a set of instructions until a condition is satisfied.

    Example Print numbers from 1 to 10 using a loop.
    4

    Recursive Algorithm

    Solves a problem by calling itself with smaller inputs

    A Recursive Algorithm solves a large problem by breaking it down into smaller sub-problems and calling itself repeatedly until a base condition is reached.

    Example Factorial calculation → Factorial(n) = n × Factorial(n-1).

    Basic Building Blocks — Flowchart Symbols

    Algorithms are often represented visually using flowcharts. Flowcharts use standard symbols to depict different types of instructions.

    Symbol Name Purpose
    Oval / Rounded Rectangle Terminator Indicates Start or Stop.
    Parallelogram Input / Output Used for input or output operations.
    Rectangle Process Represents a processing step or instruction.
    Diamond Decision Used for decision making (Yes / No).
    Arrow Flow Line Shows the direction of flow of control.
    Circle Connector Connects different parts of a flowchart.

    Real-Life Examples of Algorithms

    Algorithms are not just for computers — they are all around us in daily life. Here are some familiar real-life examples.

    Making a Cup of Tea

    • Boil water
    • Add tea leaves
    • Add milk and sugar
    • Pour into cup and serve

    ATM Cash Withdrawal

    • Insert ATM card
    • Enter PIN
    • Choose amount
    • Collect cash and receipt

    Traffic Signal System

    • Red → Stop
    • Yellow → Slow down
    • Green → Go
    • Follows fixed timing sequence

    Online Ticket Booking

    • Choose source and destination
    • Select date and passengers
    • Choose seat and pay
    • Receive confirmation

    Recipe Preparation

    • Gather ingredients
    • Follow steps in order
    • Cook for required time
    • Serve the dish

    Google Search

    • Enter search query
    • Google algorithm processes it
    • Relevant results are ranked
    • Displays top matches

    How to Write a Good Algorithm

    Writing an effective algorithm requires clarity, logic, and simplicity. Follow these steps to design a strong algorithm.

    Steps to Write an Algorithm

    • Understand the problem clearly before starting.
    • Identify the inputs and expected outputs.
    • Break the problem into smaller logical steps.
    • Use simple, clear language — avoid jargon.
    • Ensure the algorithm is finite and effective.
    • Test your algorithm with different inputs.
    • Refine and optimize for better performance.
    • Use flowcharts or pseudocode for visualization.

    Algorithm Example in Pseudocode

    Here's an example algorithm to check whether a number is even or odd, written in pseudocode.

    Step 1: Start
    Step 2: Read a number N
    Step 3: If N % 2 == 0, then
            Print "N is Even"
            Else
            Print "N is Odd"
    Step 4: Stop
    Explanation The algorithm reads a number, checks if it's divisible by 2, and prints whether it is Even or Odd.

    Algorithm vs Program vs Flowchart

    Beginners often confuse these three concepts. Here's a quick comparison.

    Aspect Algorithm Program Flowchart
    Definition Step-by-step logic Coded implementation Visual diagram
    Form Written in plain language Written in programming language Uses standard symbols
    Executed On Not directly executable Executed on a computer Not executable
    Purpose Design logic Run the logic Visualize the logic

    Did You Know?

    Interesting Fact

    The word "Algorithm" comes from the name of the Persian mathematician Muhammad ibn Musa al-Khwarizmi, who lived in the 9th century. His work on arithmetic and algebra laid the foundation for modern mathematical algorithms.

    Frequently Asked Questions

    Q1. What is an algorithm in simple words?

    An algorithm is a set of clear, step-by-step instructions used to solve a problem or complete a task.

    Q2. What are the characteristics of a good algorithm?

    A good algorithm must be finite, definite, effective, accept inputs, and produce at least one output.

    Q3. What is the difference between an algorithm and a program?

    An algorithm is a plan or logic to solve a problem, while a program is the actual code written in a programming language to implement the algorithm.

    Q4. What are the different types of algorithms?

    The main types are Sequential, Conditional, Iterative, and Recursive algorithms.

    Q5. Why are algorithms important?

    Algorithms are important because they provide efficient solutions to problems, optimize resources, and form the foundation of every software and system in the digital world.

    Key Takeaways

    • An algorithm is a step-by-step solution to a problem.
    • It must be finite, definite, and effective.
    • Algorithms are the foundation of programming.
    • Good algorithms lead to efficient and optimized solutions.
    • Practice and logic are the keys to writing better algorithms.
    • Every program is essentially the implementation of an algorithm.
    • Algorithms are used everywhere — from Google search to daily life.

    Why Algorithms Matter

    Algorithms are everywhere! They power computers, smartphones, applications, and even simple everyday tasks. Understanding algorithms helps in problem-solving, critical thinking, and building innovative solutions. Mastering algorithms is the first step to becoming a successful programmer, developer, or computer scientist.

    Master Logic, Think Smarter, Solve Better! 🚀