0% found this document useful (0 votes)
11 views6 pages

Python Class2

The document outlines the fundamental building blocks of algorithms, including inputs and outputs, sequence, selection, iteration, functions, recursion, and data structures. It also discusses the use of pseudocode for algorithm design, providing examples and basic notations, as well as the importance of flowcharts in visually representing algorithms. Overall, it emphasizes the significance of these concepts in creating efficient problem-solving techniques in computing.

Uploaded by

mchendru2008
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Python Class2

The document outlines the fundamental building blocks of algorithms, including inputs and outputs, sequence, selection, iteration, functions, recursion, and data structures. It also discusses the use of pseudocode for algorithm design, providing examples and basic notations, as well as the importance of flowcharts in visually representing algorithms. Overall, it emphasizes the significance of these concepts in creating efficient problem-solving techniques in computing.

Uploaded by

mchendru2008
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Index

1. Building blocks of algorithm


2. Notation and pseudocode
3. Flowchart

I Building Blocks of Algorithms


Algorithms are constructed using fundamental building blocks that define their structure and
functionality. These building blocks help in designing efficient and logical problem-solving
techniques. The key building blocks of algorithms include:

1. Inputs and Outputs


 Input: The data or values provided to an algorithm before execution.
 Output: The result produced by the algorithm after processing the input.
 Example: In a sorting algorithm, the input is an unsorted list, and the output is the sorted list.

2. Sequence (Order of Execution)


 The sequence defines the step-by-step execution of instructions in a linear manner.
 Every instruction is executed once before moving to the next step.
 Example:
x = 5
y = 10
sum = x + y
print(sum) # Output: 15

3. Selection (Decision Making - Conditional Statements)


 Allows the algorithm to make decisions based on conditions.
 Uses if-else statements to choose between different paths.
 Example (Finding the largest number):
a, b = 10, 20
if a > b:
print("A is greater")
else:
print("B is greater") # Output: B is greater

4. Iteration (Loops - Repeating Instructions)


 Enables repetition of certain steps until a condition is met.
 Uses for loops and while loops.
 Example (Print numbers from 1 to 5 using a loop):
for i in range(1, 6):
print(i)
# Output: 1 2 3 4 5

5. Functions (Modularization & Reusability)


 Helps break large algorithms into smaller reusable parts.
 Enhances readability, reusability, and debugging.
 Example (Function to add two numbers):
def add(x, y):
return x + y

result = add(5, 3)
print(result) # Output: 8

6. Recursion (Self-Calling Functions)


 A function calls itself to solve smaller instances of a problem.
 Used in problems like Fibonacci series, factorial computation, and tree traversals.
 Example (Factorial using recursion):
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)

print(factorial(5)) # Output: 120

7. Data Structures (Organizing Data Efficiently)


 Algorithms use data structures like:
 Arrays – For storing multiple values in a sequence.
 Linked Lists – For dynamic memory allocation.
 Stacks & Queues – For LIFO (Last In, First Out) & FIFO (First In, First Out)
operations.
 Graphs & Trees – For hierarchical and network-based problems.

Conclusion
These fundamental building blocks form the backbone of algorithm design. A well-structured
algorithm efficiently utilizes these concepts to solve complex problems in computing.
II Notation & Pseudocode in Algorithms
When designing algorithms, we use notations and pseudocode to describe the steps in a structured
yet readable way. Pseudocode is a simplified, language-independent way of writing algorithms
before implementing them in a programming language.

1. What is Pseudocode?
 A high-level description of an algorithm.
 Uses simple English-like statements without syntax rules of programming languages.
 Helps in understanding logic before coding.

2. Basic Notations in Pseudocode


Concept Pseudocode Representation
Input READ x or INPUT x
Output PRINT x or DISPLAY x
Assignment x ← 10 (Assign value 10 to x)
Decision IF condition THEN ... ELSE ... ENDIF
Loops FOR, WHILE, REPEAT UNTIL
Functions FUNCTION name(parameters)

3. Example Pseudocode for Common Algorithms


a) Pseudocode for Finding the Largest of Two Numbers
BEGIN
INPUT a, b
IF a > b THEN
PRINT "A is larger"
ELSE
PRINT "B is larger"
ENDIF
END

b) Pseudocode for Summing Numbers from 1 to N


BEGIN
INPUT N
SUM ← 0
FOR i ← 1 TO N DO
SUM ← SUM + i
ENDFOR
PRINT "Sum is", SUM
END
c) Pseudocode for Factorial (Using Recursion)
FUNCTION Factorial(N)
IF N = 0 THEN
RETURN 1
ELSE
RETURN N * Factorial(N-1)
ENDIF
END FUNCTION

4. Why Use Pseudocode?


✅ Easy to understand without coding knowledge
✅ Focuses on logic rather than syntax
✅ Helps in debugging and algorithm optimization

III Flowchart in Algorithms


A flowchart is a graphical representation of an algorithm that uses symbols to illustrate the flow of
execution. It helps in visually understanding the logic of an algorithm before implementation.

1. Importance of Flowcharts
✅ Simplifies Complex Logic – Easy to understand and debug
✅ Improves Clarity – Visual representation of steps
✅ Language-Independent – Can be used before coding in any language
✅ Standardized Representation – Common symbols make it universally understandable

2. Basic Flowchart Symbols


Symbol Name Purpose
Oval Start/End Represents the beginning or end of a flowchart
Parallelogram Input/Output Used for user input (e.g., entering a value) or displaying output
Rectangle Process Represents a process or calculation (e.g., sum = a + b)
Diamond Decision Represents a decision (e.g., IF condition THEN)
➝ Arrow Flow Direction Shows the flow of the algorithm

3. Example Flowcharts
a) Flowchart for Finding the Largest of Two Numbers
[Link]:
1. Start
2. Input two numbers (A, B)
3. Compare A and B
4. If A > B, print "A is larger"; otherwise, print "B is larger"
5. End
[Link]:
Start

Input A, B

A > B?
/ \
Yes No
| |
Print Print
"A is "B is
larger" larger"
\ /

End

b) Flowchart for Summing Numbers from 1 to N


[Link]:
1. Start
2. Input N
3. Initialize sum = 0
4. Loop from i = 1 to N
5. Add i to sum
6. Print sum
7. End
[Link]:
Start

Input N

Sum ← 0

i ← 1

i ≤ N?
/ \
Yes No
| |
Sum = Print Sum
Sum + i ↓
| End
i ← i+1
\ /

Loop
4. When to Use Flowcharts?
✅ Before writing code to visualize the algorithm
✅ For documentation to help others understand the logic
✅ For debugging and troubleshooting

You might also like