ALGORITHM
1. What is an Algorithm?
•Definition: An algorithm is a step-by-step procedure or formula for solving a
problem. It takes input, processes it, and produces output.
•Characteristics:
• Finiteness: An algorithm must terminate after a finite number of steps.
• Definiteness: Each step must be precisely defined.
• Input and Output: It accepts input, processes it, and produces output.
• Effectiveness: All operations must be basic enough to be performed, in theory, by a
person with paper and pencil.
An algorithm has several key features that define its behavior and effectiveness. Here are the main features of an algorithm:
1. Finiteness
Definition: An algorithm must always terminate after a finite number of steps.
Importance: This ensures that an algorithm does not run indefinitely. It must eventually stop and produce an output.
2. Definiteness
Definition: Every step in an algorithm must be precisely and unambiguously defined.
Importance: Clear instructions ensure that the algorithm can be followed without confusion, preventing errors in
implementation.
3. Input
Definition: An algorithm takes some input (or multiple inputs), which is provided before or during its execution.
Importance: Input is necessary because the algorithm uses it to perform operations and produce an output.
4. Output
Definition: An algorithm must produce at least one output, which is the result of its operations.
Importance: The output reflects the problem-solving process and ensures that the algorithm solves the intended problem.
5. Effectiveness
Definition: Each step in the algorithm must be basic enough to be carried out, in theory, by hand (or a simple machine like a
calculator).
Importance: The operations should be simple and feasible for humans or computers to execute, making the algorithm
practical.
6. Generality
Definition: An algorithm should be applicable to a class of problems, not just a single
instance.
Importance: A good algorithm is not just hardcoded to solve one specific case but can be
adapted to solve similar types of problems.
7. Efficiency
Definition: An algorithm should ideally solve the problem in the least amount of time and
using the least amount of resources (such as memory).
Importance: Efficiency is crucial for handling large inputs, as inefficient algorithms can
become slow or consume too many resources.
8. Determinism
Definition: An algorithm should produce the same output for a given input every time it
is executed.
Importance: This ensures consistency and reliability in the behavior of the algorithm.
9. Clarity
Definition: The steps of the algorithm should be easy to understand and logically
structured.
Importance: A clear algorithm is easier to implement, test, and debug.
examples
Factorial of a Number
Problem: Find the factorial of a number (n! = n × (n-1) × ... ×
1).
Algorithm:
Step 1:Start
Step 2:Get the number in n.
Step3:Initialize result =1 and i=1
Step 4:Repeat the step 5 to step 6 for n times
Step 5:Multiply the result by i
Step 6:Increment i by 1
Step 7:Return the final result.
Step 8: Stop
Algorithm: Find the Largest of 3 Numbers
Step 1: start
Step 2:Take three numbers as input.
Step 3:Compare the first two numbers:
If the first number is greater than or equal to the second, proceed to step 4..
Otherwise, proceed to step 5.
Step 4:Compare the larger of the first two numbers with the third number:
If the larger number is greater than or equal to the third number, that’s the
largest.
Step 5:If the second number is greater than the first, compare the second number
with the third number:
If the second number is greater than or equal to the third, that’s the largest.
Otherwise, the third number is the largest.
Step 6:stop
Step1:Start
Step2:Input numbers (a, b, c)
Step3:Is a >= b?
If yes, go to step 4.
If no, go to step 5.
Step 4:Is a >= c?
If yes, output "a is the largest" and go to End.
If no, output "c is the largest" and go to End.
Step 5:Is b >= c?
If yes, output "b is the largest" and go to End.
If no, output "c is the largest" and go to End.
Step6:stop
FLOWCHART
Flowchart:
A flowchart is a graphical representation of a process or algorithm. It uses standardized symbols and arrows to depict
the flow of control and data in a step-by-step manner. Flowcharts are widely used in problem-solving, process
management, and programming because they visually represent the logic of a process or algorithm.
Key Symbols in a Flowchart:
Oval (Terminal)Purpose:
Used to represent the start or end of a [Link]: "Start" or "End".Shape: Ellipse or rounded rectangle.
Rectangle (Process)Purpose:
Represents a process or operation (e.g., a calculation, assignment, or any step where something happens).Example:
"Add two numbers", "Set variable X to 5".Shape: Rectangle.
Parallelogram (Input/Output)Purpose:
Indicates input from the user or output to the user (e.g., data entry or displaying results).Example: "Enter number",
"Display result".Shape: Parallelogram.
Diamond (Decision)Purpose:
Represents a decision point, where the flow can go in two directions based
on a [Link]: "Is X > Y?" (If yes, go one way; if no, go the
other).Shape: Diamond.
Arrow (Flow Line)Purpose:
Shows the direction or flow of control from one step to the [Link]: An
arrow connecting steps, representing the order of [Link]: Arrow.
Circle (Connector)Purpose:
Used to connect different parts of the flowchart, especially when a flowchart
spans multiple pages or [Link]: To indicate a jump to another part
of the [Link]: Small [Link]
Flowchart Structure
:Start/End: The flowchart begins and ends with an oval or terminal
symbol
.Process Steps: Rectangles represent the tasks or operations performed
.Decision Points: Diamonds are used to represent decisions that branch
the flow based on conditions
.Input/Output: Parallelograms show where data is input or output.
Advantages of Using Flowcharts:
• Easy to Understand:Flowcharts offer a simple, intuitive way to visualize processes and
algorithms.
• Communication:They provide a clear way to communicate how a system works or how a
problem is solved
• .Problem-Solving:Flowcharts help break down complex processes into smaller, manageable
steps.
• Error Detection:Flowcharts help identify bottlenecks or errors in logic, especially in algorithms.
• Documentation:Flowcharts can serve as documentation for software systems, algorithms, or
workflows.
Disadvantages of Flowcharts:
• Scalability:Flowcharts can become very large and complex when depicting intricate processes,
making them hard to manage.
• Rigidity:Changes in the process might require rewriting parts of the flowchart, which can be
time-consuming.
• Limited Details:Flowcharts provide an overview of a process but do not capture all the details
like code or specific data structures might.