Unit 2
Algorithmic Thinking
Algorithmic Thinking is a logical way of solving problems by breaking them down into small,
step-by-step instructions that can be followed systematically to reach a solution. It
involves decomposition, where a complex task is divided into smaller manageable parts,
sequencing, where steps are arranged in a proper order, abstraction, where unnecessary
details are ignored, and pattern recognition, where similarities are identified to solve
problems efficiently. Algorithmic thinking is widely used in programming, mathematics,
and everyday problem-solving. For example, making tea can be expressed algorithmically
by defining steps such as boiling water, adding tea leaves, mixing sugar and milk, and
finally pouring into a cup. This structured way of thinking helps in creating clear
instructions that can be easily understood and followed.
Flowchart
A Flowchart is a diagrammatic representation of an algorithm or process that uses
standard symbols to show the sequence of steps. It illustrates the logic of solving a
problem in a visual format, making it easier to understand and analyze. In a flowchart,
different shapes represent different actions, such as an oval for start or end, a rectangle for
processes, a diamond for decision-making, and a parallelogram for input or output. Arrows
are used to indicate the direction of flow from one step to another. Flowcharts are
important because they simplify complex processes, help in planning programs before
coding, assist in debugging and documentation, and improve communication between
programmers and users. For example, a flowchart for adding two numbers would begin
with a start symbol, then take input values, perform the addition process, display the
result, and finally end the process.
Advantages of Flowcharts
1. Easy to Understand
o Flowcharts use symbols and arrows, which make the logic of a process clear
and simple.
o Even people with little or no programming knowledge can understand the
steps easily.
2. Visual Representation of Logic
It provides a visual way of representing an algorithm, which is often easier to follow than
lengthy text descriptions.
oThe flow of control (sequence, decision, looping) can be seen clearly.
3. Helps in Problem Analysis
• Flowcharts break down a problem into smaller steps, making it easier to analyze
and identify issues.
• It allows programmers to visualize the entire process before actual coding.
4. Useful in Debugging
• Errors can be easily located by tracing the flow of the chart.
• It helps in checking the correctness of logic before implementing the code.
5.Effective Communication Tool
• Flowcharts are commonly used for discussion among team members,
programmers, or between clients and developers.
• They serve as a common language for understanding processes.
6. Facilitates Documentation
• Flowcharts act as proper documentation for programs or systems.
• Future maintenance becomes easier as new developers can understand the system by
referring to the flowchart.
7. Saves Time and Effort
• Since flowcharts clearly define the logic, programmers save time in coding and testing.
• It reduces confusion and rework.
8. Helps in Program Maintenance and Modification
• When changes are required, the flowchart helps in identifying which part of the logic
needs modification.
• This ensures smooth updates and maintenance.
Flowchart Symbols
Examples of Flowchart
Sequential : A Sequential flowchart represents a process in which steps are executed
one after another in a straight order without any branching or repetition. It is the simplest
form of flowchart.
Flow: Start → Step 1 → Step 2 → Step 3 → … → End
Use: When instructions are fixed and do not require conditions or repetition.
Example- Addition of two numbers, To find area of circle, calculate Simple Interest
Branching : A Decision flowchart is used when a process requires choosing between two
or more paths based on a condition. It makes use of the diamond symbol for decision-making.
Flow: Start → Condition → (Yes/No or True/False) → Different paths → End
Use: When logic depends on conditions.
Example: To check if number is even or odd, Finding largest among two numbers.
Looping: A Looping flowchart is used when a process or set of instructions needs to be
repeated multiple times until a condition is satisfied. It represents iteration (repetition).
Flow: Start → Condition → Repeat steps (Yes) → Re-check condition → Exit loop (No) → End
Use: When tasks must be performed again and again, such as printing numbers,
calculating factorial, etc.
Examples: To print n natural numbers, calculate factorial of a number
Algorithm
An Algorithm is a finite, step-by-step procedure or set of well-defined instructions designed to
solve a specific problem or perform a particular task. It takes some input, processes it
systematically, and produces an output.
Algorithms are the foundation of computer programming because they provide a logical
sequence of operations that a computer can follow to reach the correct solution.
Characteristics of Algorithms
• Finiteness
An algorithm must always finish after a certain number of steps. It should not go into an endless
loop. This ensures that the problem will be solved in a reasonable amount of time. For example,
if you write an algorithm to calculate the sum of the first 100 numbers, it must stop after
performing exactly 100 additions. If an algorithm does not stop, it cannot be considered a proper
algorithm.
• Definiteness
Every step of an algorithm must be clearly and unambiguously defined. There should be no
confusion about what is to be done in each step. For example, instead of writing “process the
data,” the step should clearly state “add the two numbers A and B.” This characteristic makes
sure that the algorithm can be followed exactly in the same way every time without
misinterpretation.
• Input
An algorithm should accept zero or more inputs. Inputs are the values or data provided to the
algorithm to work on. Without input, an algorithm may not be able to produce meaningful
results. For example, a sorting algorithm requires a list of numbers as input, and a search
algorithm needs the element to be searched along with the list.
• Output
An algorithm must produce at least one output, which is the solution to the given problem. The
output is the final result obtained after executing all the steps of the algorithm. For example, in
an algorithm for finding the largest of two numbers, the output will be the greater number.
Without output, the algorithm does not serve its purpose.
• Effectiveness
Each step of an algorithm must be simple, basic, and executable within a finite time using
available resources. This means that the operations in the algorithm should be clear and feasible.
For example, “add two numbers” is an effective step because it can be carried out easily, but
“calculate the most beautiful painting in the world” is not effective because it cannot be
practically executed.
• Generality
An algorithm should be general enough to solve a group of problems, not just one specific
instance. This means it should work for all valid inputs within the problem’s scope. For example,
an algorithm for multiplying two numbers should be able to multiply any pair of numbers, not
just 2 × 3. This makes the algorithm reusable and useful in many situations
Sequencing Algorithm
Definition:
A Sequencing Algorithm is an algorithm in which instructions are executed one after another
in a specific order, step by step. There is no decision-making or looping involved; each step is
carried out exactly in sequence until the task is completed.
Characteristics of Sequencing Algorithm
1. Each step is executed exactly once.
2. There are no conditions or loops.
3. Works well for simple, linear tasks.
Problem: Add two numbers and display the result.
Algorithm (Sequencing):
1. Start
2. Read the first number A
3. Read the second number B
4. SUM = A + B
5. Display SUM
6. Stop
Explanation:
• Step 2 is executed first, then Step 3, then Step 4, and so on.
• No decisions are made; steps are performed in linear order.
Example 2: Making Tea
Algorithm (Sequencing):
1. Start
2. Boil water
3. Add tea leaves to the water
4. Let it boil for 2–3 minutes
5. Pour tea into cup
6. Add sugar and milk as needed
7. Serve tea
8. Stop
Explanation:
• Each step is executed one after another.
• There is no choice or repetition.
• This is a real-life example of a sequencing algorithm.
Selection Algorithm
Definition :
A Selection Algorithm is an algorithm in which decisions are made based on certain
conditions. It allows the program to choose different actions depending on the input or
situation. This type of algorithm uses conditional statements like if, if-else, or switch.
Characteristics of Selection Algorithm
1. Involves decision-making.
2. Can have two or more possible paths depending on the condition.
3. Produces different outputs for different inputs.
Example 1:
Problem: Find the larger number between A and B.
Algorithm (Selection):
1. Start
2. Read two numbers A and B
3. If A > B
o Display “A is largest”
Else
o Display “B is largest”
4. Stop
Explanation:
• Here, the condition A > B is checked.
• If it is true, one set of instructions runs; if false, another set runs.
• This is the decision-making part of the algorithm.
Example 2:
Problem: Check if a number is even or odd.
Algorithm (Selection):
1. Start
2. Read number N
3. If N % 2 == 0
o Display “N is Even”
Else
o Display “N is Odd”
4. Stop
Explanation:
• The algorithm selects the path depending on whether the condition (N % 2 == 0) is true
or false.
• Only one branch is executed at a time.
Iteration (Looping) Algorithm
Definition:
An Iteration Algorithm is an algorithm in which a set of instructions is repeated multiple
times until a certain condition is met. This type of algorithm uses loops like for, while, or
do-while.
Characteristics of Iteration Algorithm
1. Involves repetition of steps.
2. Has a condition to continue or terminate the loop.
3. Can handle tasks that require multiple repetitions efficiently.
Example1:
Problem: Print numbers from 1 to 5.
Algorithm (Iteration):
1. Start
2. Set counter i = 1
3. While i ≤ 5
o Print i
o Increment i by 1
4. Stop
Explanation:
• Steps 3(a) and 3(b) are repeated 5 times.
• The loop stops when the condition i ≤ 5 becomes false.
Example 2:
Problem: Find the sum of first N natural numbers.
Algorithm (Iteration):
1. Start
2. Read number N
3. Set SUM = 0 and i = 1
4. While i ≤ N
o SUM = SUM + i
o i=i+1
5. Print SUM
6. Stop
Explanation:
• The loop repeats adding numbers from 1 to N.
• Efficiently handles repetitive addition without writing multiple steps manually.