Algorithm
An algorithm is the basic logic of a program. It is a step-by-step procedure or
set of instructions to solve a specific problem or perform a task and at the end
we get our desired result.
Ex: Algorithm for making tea
Step 1: Gas ON (start)
Step 2: Boil water
Step 3: Add sugar, milk, tea leaves etc.
Step 4: Stir and mix
Step 5: Gas OFF (end)
Properties of algorithm:
1. Input: An algorithm can have zero or more clearly defined inputs.
2. Output: An algorithm must always produce at least one output.
3. Unambiguous/ Unambiguity: Every instruction in the algorithm must be clear
and straightforward. There should not be any confusion.
Ex:
Read a (clear instruction to read the variable a).
Read alone is unclear and has no meaning.
4. Finiteness: An algorithm should always finish after a set number of steps.
Ex:
while(true) {
. . . .
. . . .
}
This leads to an infinite loop because the condition is always true.
5. Effectiveness: Each step of the algorithm should affective or something
useful. No unnecessary steps.
Step 1: Start Step 1: Start
Step 2: Read a Step 2: Read a
Step 3: Read b Step 3: Read b
Step 4: c = a + b Step 4: Read d
Step 5: print c Step 5: c = a + b
Step 6: end Step 6: print c
Step 7: end
In this algorithm
every step is effective Here step 4(Read d) is not necessary.
and useful. It is not useful.
Algorithm vs Program
Algorithm Program
Used in design phase. Used in implementation phase.
Use natural(human) language. Use programming language.
To write an algorithm person should Any programmer can write program
have domain knowledge. according to algorithm.
Not dependent on h/w and s/w. Dependent on hardware and software.
No need of h/w and s/w. Require h/w and s/w.
Used for analysis purpose. Used for testing purpose.
Ex: Multiplication of two numbers
Step 1: Start
Step 2: read a, b
Step 3: multi= a*b
Step 4: print multi
Step 5: stop
Avoiding Infinite loops
To avoid infinite loops in algorithms, two common methods can be used:
1. Counting
2. Sentinel values.
1. Avoiding Infinite Loops by Counting
In this method, you use a counter to keep track of how many times a loop
has run. When the counter reaches a specific value (often a limit you've
set), the loop stops. This is helpful when you know how many iterations
you want to run.
2. Avoiding Infinite Loops by Using Sentinel Values
A sentinel value is a special value that indicates the end of a loop. The
loop continues until it encounters this value. This is useful when you
don’t know how many iterations will be needed, but you know the loop
should stop when a specific condition is met.
Summary:
Counting limits the number of iterations directly.
Sentinel values stop the loop when a specific condition is met.
Representation of algorithms
Common ways to represent an algorithm:
1. As a program
2. As a Flowchart
3. As a Pseudo Code
Flowchart
A flowchart is a graphical representation of an algorithm or a problem.
It is the first step towards solving a problem.
Flowcharts make it easier for everyone to understand how a process works.
Key Features of a Flowchart:
Uses simple symbols to show steps in a process.
Breaks down a problem into easy-to-follow steps.
Helps you see how things are connected and how the system works.
Makes it easier to analyze and fix issues in a process.
Simple and clear, so anyone can understand how a process works.
Turns a big problem into smaller parts that are easier to handle.
Shapes used to draw Flowchart:
1. Oval start/ end
2. Rectangle processing
3. Parallelogram input/ output
YES NO
4. Diamond condition/ decision making
5. Arrow/ Flow lines direction
6. Circle/ Connector used for page connection
Example of flowchart
Addition of two numbers
Using steps: using flowchart:
Step 1: Start
Step 2: Input a, b
Step 3: c = a + b
Step 4: print c
Step 5: end
Division flowchart: