0% found this document useful (0 votes)
4 views121 pages

Algorithm & Data

The document covers the fundamentals of algorithms, flowcharts, and pseudocode, emphasizing their roles in problem-solving and programming. It outlines objectives for learners, including understanding algorithms, searching algorithms, and decomposition, while providing examples and advantages of algorithms. Additionally, it details flowchart symbols and rules for writing pseudocode, highlighting its importance in algorithm design and programming.

Uploaded by

flamelord9t
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)
4 views121 pages

Algorithm & Data

The document covers the fundamentals of algorithms, flowcharts, and pseudocode, emphasizing their roles in problem-solving and programming. It outlines objectives for learners, including understanding algorithms, searching algorithms, and decomposition, while providing examples and advantages of algorithms. Additionally, it details flowchart symbols and rules for writing pseudocode, highlighting its importance in algorithm design and programming.

Uploaded by

flamelord9t
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

ALGORITHMS AND DATA

• Algorithms,
• Flowcharts,
• Pseudocode,
• Construct,
• Conditional Statement,
• Searching Algorithm,
• Linear Search,
• Decomposition / Decompose &
• Constant
OBJECTIVES
▪ Learners will extend their experience of following
algorithms when they are presented as flowcharts.

▪ They will also be introduced to the concept of


pseudocode and its purpose in designing algorithms.
This will include developing an understanding of the
principles of pseudocode and of following
pseudocode algorithms that include conditional
statements.
OBJECTIVES

▪ Learners will also be introduced to the concept of


searching algorithms and to the reasons why these are
required. They will gain practical experience of using a
linear search.

▪ Learners will also extend their understanding of the


computational thinking concept of decomposition, and
its role in reducing problems into smaller problems that
can be solved independently of each other.
WHAT IS ALGORITHM?
▪A sequence of steps to solve a given
problem is called as algorithm.
▪An algorithm is a step-by-step
procedure developed for solving a
given problem.
▪An algorithm consists of: sequences,
iterations, selections, etc.
EXAMPLES OF ALGORITHM
▪An algorithm follows a systematic and a logical
approach, where the procedure is defined step-
wise (that is step-by-step).

▪ For Example: how to make a phone call


▪ Step 1: Start algorithm
▪ Step 2: Turn on phone or not (when phone is already on)
▪ Step 2: Dial the phone number
▪ Step 3: Wait for the phone to ring
▪ Step 4: Start talking
▪ Step 5: End algorithm
EXAMPLES OF ALGORITHM
▪ Example 2: how to use the atm card to withdraw
money from the atm machine.

▪ Step 1: Start algorithm


▪ Step 2: Go to any local ATM
▪ Step 2: Insert or swipe ATM Card in the ATM machine
▪ Step 3: Enter your PIN Code Number
▪ Step 4: Press the account type (current or Savings) from
which you want to withdraw the money
▪ Step 5: Press the cash withdrawal button
EXAMPLES OF ALGORITHM
▪Step 6: Enter the amount you want to
withdraw
▪Step 7: Collect the amount from the ATM
machine
▪Step 8: Collect the statement from the ATM
machine
▪Step 9: Collect the ATM Card
▪Step 10: End of algorithm
EXAMPLES OF ALGORITHM
▪Example 7:
Write an algorithm to multiply two numbers.
EXAMPLES OF ALGORITHM
Solution
▪Step 1: Start the Algorithm
▪Step 2: Get the first Number
▪Step 3: Get the second Number
▪Step 4: Multiply the two Numbers
▪Step 5: Display the result.
▪Step 4: Stop the Algorithm
EXAMPLES OF ALGORITHM
Home Work
▪Write an algorithm to accept two numbers.
Divide the first number by the second number
and display their quotient.

▪Write an algorithm that accept 5 numbers and


display the sum and average of the numbers.
ADVANTAGES OF ALGORITHM
▪It is a step-wise representation of a solution to a
given problem, which makes it easy to understand.
▪An algorithm uses a definite procedure.
▪It is not dependent on any programming language,
so it is easy to understand for anyone even
without programming knowledge.
▪Every step in an algorithm has its own logical
sequence so it is easy to debug.
DISADVANTAGES OF ALGORITHM

HOME WORK
ALGORITHM

▪Algorithms can be displayed or be represented by


simply as written text or more formally using
flowcharts or, when they are being designed to be
used for a computer, as pseudocode.
ALGORITHM
▪Writing algorithms we will use following symbol for
different operations:
▪‘+’ for Addition
▪‘-’ for Subtraction
▪‘*’ for Multiplication
▪‘/’ for Division and
▪‘←’ or ‘=’ for assignment. For example A ← X*3 means A
will have a value of X*3.
ALGORITHM
Write an algorithm to read two numbers and find their sum.

Inputs to the algorithm:


▪ First num1.
▪ Second num2.
Expected output:
▪ Sum of the two numbers.
Algorithm:
▪ Step1: Start
▪ Step2: Read\input the first num1.
▪ Step3: Read\input the second num2.
▪ Step4: Sum num1+num2 // calculation of sum
▪ Step5: Print Sum
▪ Step6: End
TYPES OF ALGORITHM
There are many types of algorithm but the most
fundamental types of algorithm are:

❑Recursive algorithms
❑Dynamic programming algorithm
❑Backtracking algorithm
❑Divide and conquer algorithm
❑Greedy algorithm
❑Brute Force algorithm
❑Randomized algorithm
FLOWCHARTS
▪ The first design of flowchart goes back to 1945 which was designed by
John Von Neumann.
▪ A flowchart is a diagram that shows a sequence of steps that should
be followed.
▪ A flowchart is a diagram that depicts the “flow” of a program.
▪ Flowcharting is a tool developed in the computer industry, for
showing the steps involved in a process.
▪ A flowchart is a diagram made up of boxes, diamonds and other
shapes, connected by arrows - each shape represents a step in the
process, and the arrows show the order in which they occur.
Flowcharting combines symbols and flowlines, to show figuratively
the operation of an algorithm.
▪ The diagram could ask you to do practical activities, or it could
represent the steps that a computer program will follow.
FLOWCHARTS
▪ A flowchart is a graphical representation of an algorithm.
▪ A flowchart is a blueprint that pictorially represents the algorithm
and its steps. The steps of a flowchart do not have a specific size and
shape rather it is designed in different shapes and sizes.
▪ The flowchart is a diagram which visually presents the flow of data
through processing systems. This means by seeing a flow chart one
can know the operations performed and the sequence of these
operations in a system.
▪ These flowcharts play avital role in the programming of a problem
and are quite helpful in understanding the logic of complicated and
lengthy problems.
▪ Once the flowchart is drawn, it becomes easy to write the program in
any high level language.
FLOWCHART SYMBOLS
There are 6 basic symbols commonly used in flowcharting of
assembly language Programs:
❑Terminal,
❑Process,
❑Input/Output,
❑Decision,
❑Connector and
❑Predefined Process.
This is not a complete list of all the possible flowcharting symbols, it is
the ones used most often in the structure of Assembly language
programming.
FLOWCHART SYMBOLS
▪ Terminals
represented by rounded rectangles. Indicates the starting or ending
of the program, process, or interrupt program.
▪ Start: this is the first box in a flowchart. It has an arrow to the first box to
run.
▪ Stop: this is the last box in a flowchart. It has an arrow from the last box to
run.

START STOP
FLOWCHART SYMBOLS
▪ Input/Output Operations
▪ Used for any Input / Output (I/O) operation. Indicates that the computer is to
obtain data or output results. It is represented by parallelograms.
▪ Input: this takes a value from the user and stores it somewhere.
▪ Output: this displays a message, or makes a sound.

INPUT OUTPUT
FLOWCHART SYMBOLS
Processes
▪ Indicates any type of internal operation inside the Processor or Memory. This is
an action. It tells something to change. Used for arithmetic operations and data-
manipulations. It is represented by rectangles.

PROCESSES
FLOWCHART SYMBOLS
Decisions / Conditional
▪ Used to ask a question that can be answered in a binary format (Yes/No,
True/False). Used to represent the operation in which there are two/three
alternatives, true and false etc. Represented as a diamond(rhombus).
▪ Indicates a decision point between two or more paths in a flowchart.

DECISION
FLOWCHART SYMBOLS

Connector / On Page Connector


▪ Connects two or more parts of a flowchart, which are on the same page.
Indicates an inspection point.
FLOWCHART SYMBOLS

Off Page Connector


Off-page Connector: An off-page connector is used when the target is on another
page. This is used to connect two parts of a flowchart which are spread over
different pages.
FLOWCHART SYMBOLS
Preprocessor / Loop
Indicates a modification to a process, such as setting a switch or initializing a
routine.
This symbol looks like a hexagon. This symbol is used for implementing loops.
Four flow lines are associated with this symbol. Two lines are used to indicate the
sequence of the program and the remaining two are used to show the looping area,
that is from the beginning to the end. Loop symbol is as shown below:
FLOWCHART SYMBOLS

Comment, Annotation Symbol.


The symbol shown below represents the annotation function, that is, the addition
of descriptive comments or explanatory notes as clarification. The broken line is
connected to any symbol at a point where the annotation is meaningful by
extending the broken line in whatever fashion is appropriate.
FLOWCHART SYMBOLS

Flow Lines (Arrow, Connector)


Flow line Used to indicate the flow of logic by connecting symbols. Shows
direction of flow. Flow line connectors show the direction that the process flows.
FLOWCHART SYMBOLS

Delay
Indicates a delay in the process.
FLOWCHART SYMBOLS

Display
The symbol shown below represents an I/O function in which the information is
displayed for human use at the time of processing, by means of online indicators,
video devices, console printers, plotters, and so forth.
FLOWCHART SYMBOLS

Documents
This shape shows a Document in a flowchart.
FLOWCHART SYMBOLS

Subroutine
Indicates a sequence of actions that perform a specific task embedded within a
larger process. This sequence of actions could be described in more detail on a
separate flowchart. Indicates a predefined (named) process, such as a subroutine
or a module.
FLOWCHART
A flowchart has to follow set rules. These rules include:
• specific shapes for different purposes:
▪ input – for the user to enter data
▪ process – to do a calculation or action
▪ output – to display words and numbers to the user
▪ a start and a stop (sometimes these might be labelled
‘begin’ and ‘end’)
▪ data flows (the arrows that show you how to move through
▪ the flowchart).
FLOWCHART
A data flow is an arrow. The arrows show you which box to
move to next.
FLOWCHART

The start box does not have any arrows going into it,
but it must have one (and only one) arrow coming
from it.
The stop box has one (and only one) arrow going into it, and
no arrows coming from it.
The input/output box has one (and only one) arrow going
into it, and one (and only one) arrow coming from it.
The process box has one (and only one) arrow going into it,
and one (and only one) arrow coming from it.
FLOWCHART
This flowchart uses a variable. A variable is a named
storage location where you can store data. The data value
can change in a variable.
In this flowchart, the variable is age. The flowchart can put
data into age, for example: How to calculate someone’s age
in ten years now when given the current age.
FLOWCHART SYMBOLS
Here is a flowchart to calculate the average of two numbers.
PSEUDOCODE
▪ Pseudocode simply means there is no specific syntax that
you have to use. It doesn’t have to follow a set format, but it
is more structured than English sentences.
▪ Pseudocode: a language that is used to display an
algorithm
▪ Pseudocode is a kind of structured English for describing
algorithms. It is a generic, code-like language that can be
easily translated into any programming language.
▪ Pseudocode: a form of representing an algorithm without a
set syntax.
▪ Syntax refers to the rules and guidelines that define the
structure and composition of a programming language. it acts
as a blueprint for writing code, ensuring the computer
understands the instructions.
PSEUDOCODE
▪A simple form of an algorithm is a series of steps to
move someone (or something) through a maze. The
instructions you can use are:
PSEUDOCODE
▪Write an algorithm to move from Start to Finish
PSEUDOCODE
▪Write an algorithm to move through the maze below:
PSEUDOCODE
▪Write an algorithm to move through the maze below:
PSEUDOCODE
▪Write an algorithm to move through the maze below:
WHEN IS PSEUDOCODE USED?
▪ Pseudocode is used during planning or designing
algorithm and development process: this is because:
❑ Designers, lead programmers and other key players can
use the code to design, collaborate on and evaluate the
logic behind the program or algorithm.
❑ Pseudocode is used to provides programmers with a
detailed template for writing code in a specific
programming language.
❑ Designers and programmers can be inspect the code as a
way to ensure that the actual programming will match
design specifications since pseudocode is written in
readable format.
WHEN IS PSEUDOCODE USED?

❑Finding errors during the pseudocode stage is less costly


than catching them later in the development process.
WHY IS PSEUDOCODE USED?
▪ Pseudocode is understood by the programmers of all
types.

▪ Pseudocode enables the programmer to concentrate


only on the algorithm part of the code development.

▪ Pseudocode cannot be compiled into an executable


program. Example, Java code : if (i < 10) { i++; }
pseudocode :if i is less than 10, increment i by 1.
FEATURES OF PSEUDOCODE
Some qualities of good pseudo code are given below:
❑It should be having a clear one starting point and
one ending point.
❑Common words such as PRINT, WRITE, READ, or
INPUT.
❑Must flow in a logical order.
❑It should be Ambiguous.
❑It should depict the logical way of solving the
problem.
❑It should be neat and legible.
FEATURES OF PSEUDOCODE
❑Pseudocode is an informal way of expressing ideas and
algorithms during the development process.

❑Pseudocode uses simple and concise words and symbols


to represent the different operations of the code.

❑Pseudocode can be used to express both complex and


simple processes.
RULES FOR WRITING PSEUDOCODE

❑ Write one statement per line.


❑ Capitalize initial keywords.
❑ Indent to show hierarchy.
❑ End multiline structure.
❑ Keep statements language independent.
PSEUDOCODE
▪Algorithms don’t go straight from start to stop. They
also need input, output, processes, decisions and
loops.
▪input: putting data into an algorithm
▪output: displaying data from an algorithm to the user
▪process: an action performed to some data to make a
change
▪decision: a comparison is used to decide if code is
run, or not
▪loop: code that is repeated
INPUT AND OUTPUT - PSEUDOCODE
▪When data is input into an algorithm it needs to
be stored somewhere. This is done using a
variable.
▪A variable is a name (identifier) that is given to
a space in a computer’s memory that stores a
value. Think of it like a box, with a name. You
could call the box myNumber. You can then put
data into myNumber. You can get data out of
myNumber, and so on.
INPUT AND OUTPUT - PSEUDOCODE

▪Variable: a space in the memory of a


computer, that has an identifier where you
can store data; this data can be changed
▪Identifier: the name given to a variable,
subroutine or function
▪Concatenate: to join two strings together.
INPUT AND OUTPUT - PSEUDOCODE
▪An output in pseudocode can use a command
word such as OUTPUT, WRITE or PRINT, for
example,
OUTPUT "Hello World“
WRITE “Hello World”
PRINT “Hellow World”

▪The words "Hello World" are a string as in the


flowcharts, so are still in speech marks.
INPUT AND OUTPUT - PSEUDOCODE
▪An input in pseudocode can use a command
word such as INPUT, ENTER, READ FROM USER,
TYPE DATA, READ LINE, READ LINE FROM USER.
❖INPUT name
❖ENTER name
❖READ FROM USER name
❖TYPE DATA name
❖READ LINE name
❖READ LINE FROM USER name
INPUT AND OUTPUT - PSEUDOCODE
▪An input in pseudocode can also be written
as: for example,
❖name = INPUT
❖name = ENTER
❖name = READ FROM USER
❖name = TYPE DATA
❖name = READ LINE
❖name = READ LINE FROM USER
INPUT AND OUTPUT - PSEUDOCODE

▪You could combine the input and output, for


example,
INPUT("Enter a number")
Or
num = INPUT("Enter a number")
INPUT AND OUTPUT - PSEUDOCODE
▪You might want to output a variable and
some text. You can combine them by using
an ampersand (&). This is to concatenate
(join) the values together.
▪For example 1 – Welcoming user by name,
▪OUTPUT "Enter your name"
▪INPUT name
▪OUTPUT "Welcome" & name
INPUT AND OUTPUT - PSEUDOCODE
▪For example 2 – Displaying username,
favorite food and favorite color,
▪INPUT name
▪INPUT favouriteColour
▪INPUT favouriteFood
▪OUTPUT "Hello ", name, " your favourite
food is ", favouriteFood, " and your favourite
colour is ", favouriteColour
PROCESSES - PSEUDOCODE
A process is an action that is performed.
If you have a number stored in a variable,
you could change this; you might add
something to it, or subtract from it.
There are different types of processes.
PROCESSES - PSEUDOCODE

modulus division:
when the remainder is
given from a division

integer division:
where only the whole
number is given from
a division
PROCESSES - PSEUDOCODE
For example,
myNumber = 10 + 3 [myNumber =13]

myNumber = myNumber + 3 [myNumber =16]

myNumber = myNumber + myNumber


[myNumber=32]

Putting a value into a variable is called assignment.


OR assignment: giving a variable a value
PROCESSES - PSEUDOCODE
▪For example Write a pseudocode needs to
ask a person’s age, then tell them how old
they will be in 50 years.

DISCUSS SOLUTION
WHAT DO YOU NEED AS A PROGRAMMER?
PROCESSES - PSEUDOCODE
▪For example Write a program needs to ask a
person’s age, then tell them how old they
will be in 50 years.
SOLUTION
OUTPUT "Enter your age" currentage =
INPUT currentage INPUT(“Enter your
age”)
newAge = currentage + 50
OUTPUT "In 50 years your will be" & newAge
PROCESSES - PSEUDOCODE
▪For example Write a pseudocode to
calculate the area of a circle when a user
enters the radius of the circle.

DISCUSS SOLUTION
WHAT DO YOU NEED AS A PROGRAMMER?
PROCESSES - PSEUDOCODE
▪For example Write a pseudocode to
calculate the new salary of an employee
after 10% increment.

DISCUSS SOLUTION
WHAT DO YOU NEED AS A PROGRAMMER?
DECISION - PSEUDOCODE
A decision is also known as comparison and
selection. It is a statement that involves a comparison
and is either true or false. If the result is true, then
some code is run, if it is false then either different
code is run or some code is skipped.
Comparison: comparing two items of data resulting in
true or false
Selection: use of a conditional statement to decide a
course of action or which section of code to run
DECISION - PSEUDOCODE
A comparison needs
Two sides of the
argument, and a
comparison symbol.
DECISION - PSEUDOCODE
SINGLE SELECTION
Selection uses the keyword IF and THEN.
The question ‘Is’ in the flowchart is
replaced with IF, and the ‘?’ with THEN.
All the code that runs when the condition
is true goes beneath the IF and this is
finished with an ENDIF to show where it
ends.
DECISION - PSEUDOCODE
SINGLE SELECTION
Syntax: IF (condition) THEN (true) ELSE
(false) ENDIF
OR
IF condition THEN
Code to run if true
ELSE
Code to run if false
ENDIF
DECISION - PSEUDOCODE
SINGLE SELECTION
Syntax: IF (condition) THEN (true) ELSE (false)
ENDIF

For example, if the value in age is more than 18,


then the output message will be displayed.

IF age > 18 THEN


OUTPUT("You are old enough")
ENDIF
DECISION - PSEUDOCODE
SINGLE SELECTION
Syntax: IF (condition) THEN (true) ELSE (false) ENDIF

An IF statement can have an ELSE. This is what happens if


the condition is true. For example, if the age is not more
than 18, then "You are not old enough" will be displayed.

IF age > 18 THEN


OUTPUT("You are old enough")
ELSE
OUTPUT("You are not old enough")
ENDIF
DECISION - PSEUDOCODE
SINGLE SELECTION
Example: 1. Write an algorithm to accept user’s
age. If the age is greater than or equal to 18,
display You are old enough. If the age is not
greater or equal to 18, display You are too Young.
2. Draw a flowchart for the above algorithm
3. Write a Pseudocode for the above flowchart
and algorithm.
DECISION - PSEUDOCODE
SINGLE SELECTION
Example: 1. Write an algorithm to display the
greater value between two numbers.
2. Draw a flowchart for the above algorithm
3. Write a Pseudocode for the above flowchart
and algorithm.
DECISION - PSEUDOCODE
MULTIPLE CONDITIONS
These can be created using ELSEIF statements.
Each condition has its own ELSEIF. You can even
put an ELSE on the end in case none of the
conditions are true. If one of the conditions is
found to be true, then the code within it runs, and
then it jumps to the ENDIF; the other conditions
after it are not checked.
DECISION - PSEUDOCODE
MULTIPLE CONDITIONS
Syntax: IF (condition) THEN (true) ELSEIF THEN (true) … ELSE (false) ENDIF

OR
IF condition THEN
Code to run if true

ELSEIF condition THEN


Code to run if true

ELSE
Code to run if false
ENDIF
DECISION - PSEUDOCODE
MULTIPLE SELECTION
Example:
cost = INPUT(“Enter The Cost”)
IF cost >= 100 THEN
OUTPUT "That is far too expensive"
ELSEIF cost >= 80 THEN
OUTPUT "That’s a bit too expensive"
ELSEIF cost >= 60 THEN
OUTPUT "That is reasonable"
ELSEIF cost >= 40 THEN
OUTPUT "That looks like a good deal"
ELSE
OUTPUT "I think that is too cheap"
ENDIF
DECISION - PSEUDOCODE
MULTIPLE SELECTION
Example: 1. Write an algorithm to display the grade
of student mark score. Below are the grading system
for shield international:
Less than 40 is “ungraded”, less than 60 mark is
“pass”, less than 80 mark is “good”, and from 80
marks and more is “outstanding”.
2. Draw a flowchart for the above algorithm
3. Write a Pseudocode for the above flowchart and
algorithm.
DECISION - PSEUDOCODE
MULTIPLE SELECTION
Example: 1. Write an algorithm to determine if a
number entered is an even number or an odd
number.
2. Draw a flowchart for the above algorithm
3. Write a Pseudocode for the above flowchart
and algorithm.
DECISION - PSEUDOCODE
MULTIPLE SELECTION
Example: 1. Write an algorithm to calculate the
following mathematical operations (“+”, “-”, “/”,
“*”) between two numbers
2. Draw a flowchart for the above algorithm
3. Write a Pseudocode for the above flowchart
and algorithm.
INPUT AND OUTPUT - FLOWCHART

QUESTIONS:

1. Explain what the flowchart is doing.

2. How many variables are in this flowchart?


PROCESS - FLOWCHART
QUESTIONS:

1. Explain what the flowchart is


doing.

2. How many variables are in


this flowchart?
PROCESS - FLOWCHART

QUESTIONS:

1. Explain what the flowchart is doing.

2. How many variables are in this flowchart?


DECOMPOSITION
Decomposition is the process of breaking down
complex systems, problems, or tasks into smaller,
more manageable and understandable
components or sub-parts.
It is often used in various fields, such as software
development, project management, and business
analysis, to simplify complex problems and
facilitate analysis, understanding, and problem-
solving by addressing individual elements
separately.
DECOMPOSITION
In systems development, decomposition refers
to the process of breaking down a complex
system or project into smaller, more manageable
components, modules, or tasks.
This approach is often used in software
development, but it can apply to various systems
development methodologies, including the
systems engineering process.
DECOMPOSITION
In computing, decomposition is the process
of breaking down a task into smaller, more-
manageable parts.
Decomposition is about breaking a problem
down into smaller parts which are more
manageable.
DECOMPOSITION
It has many advantages:
▪ It helps us manage large projects and makes the process of
solving a complex problem less daunting (difficult to deal
with) and much easier to take on.
▪ The different modules can be worked on by different people,
or teams, at the same time, which will increase productivity
▪ A small module is easier to think about and solve than a
large problem
▪ It may be possible to process modules in parallel to each
other, allowing for faster processing of the problem as a
whole.
DECOMPOSITION
Other advantages are:
1. Simplifying Complex Problems: The
primary aim of decomposition is to break
down complex problems into simpler,
manageable tasks. This simplification makes
it easier to understand and solve the
problem step by step.
DECOMPOSITION
Other advantages are:
2. Promoting Collaboration: More often
than not, large projects require multiple
individuals to work together. Decomposition
enables you to divide the work into
independent tasks that can be worked upon
in parallel, promoting effective collaboration.
DECOMPOSITION
Other advantages are:
3. Enhancing Understanding:
Decomposing a problem not only makes it
simpler to solve, but it also paves the way for
a better understanding of the overall system.
You get a clear picture of the individual
components and how they interconnect to
form the larger entity.
DECOMPOSITION

Other advantages are:


3. Improving Robustness: When problems
are decomposed, it becomes easier to test
individual parts of the system. This enhanced
testability makes the final system robust and
less prone to errors.
DECOMPOSITION
It has many disadvantages:
▪ Much time is spent because you have to
understand the problem very well to successfully
decompose it.

▪ If parts of a problem are worked on in parallel, it


may be difficult, or impossible, to recombine
them into a single solution at the end.
DECOMPOSITION
Other disadvantages are:
1. Risk of Oversimplification: While
decomposition simplifies complex problems,
there is a risk of oversimplifying the problem to
the point where the broader context or
interdependencies between subsystems may be
lost.
DECOMPOSITION
Other disadvantages are:
2. Increased Communication: Decomposition
often necessitates increased collaboration and
communication. If not managed efficiently, it can
lead to information overload, confusion, and
mistakes.
DECOMPOSITION
Other disadvantages are:
3. Requires Effective Planning: Effective
decomposition requires careful planning. Poorly
planned decomposition can result in tasks that
are still too complex, or it may neglect important
aspects of the problem.
DECOMPOSITION
Other disadvantages are:
3. Infrastructure Requirements: In some cases,
decomposition might create the need for
advanced technology or infrastructure to manage
the different subsystems separately.
DECOMPOSITION
A large computer program will often be broken
up into modules called Subprograms, or
Subroutines.
In Python, and some other programming
languages, these are often called Functions.
▪ A Function is a self contained piece of code that
can be ‘called’ and run from within other pieces
of code.
DECOMPOSITION
Decomposition can be applied in various
ways:
1. Functional decomposition: This involves
dividing a system based on the functions it
performs. For example, in a game, one
function could handle user input, another
function could process game logic, and a
third function could render graphics.
DECOMPOSITION

2. Data decomposition: In this approach,


a system is divided based on the data it
processes. This is commonly used in
parallel computing, where a large dataset
might be divided into smaller chunks that
can be processed independently.
DECOMPOSITION

3. Object-oriented decomposition: This


approach, common in object-oriented
programming, involves breaking down a
system into a collection of independent
objects that interact with each other.
DECOMPOSITION

3. Object-oriented decomposition: This


approach, common in object-oriented
programming, involves breaking down a
system into a collection of independent
objects that interact with each other.
CONSTANT IN PROGRAMMING
The constants and variables are used to store data
values in programming.
A variable generally refers to a value that can change
over time. Whereas, a constant is a type of variable
whose value cannot be changed during a program’s
execution.
There are only six built-in constants available in Python
which are False, True, None, Not Implemented, Ellipsis(
...), and __debug__.
CONSTANT IN PROGRAMMING
Example:
A sample example of a constant is demonstrated
below:
False = 100
Output
SyntaxError: cannot assign to False
CONSTANT IN PROGRAMMING
False is a built-in constant in python which stores the
boolean value false, assigning anything to it is illegal
and it will raise a SyntaxError.

Constants are capitalized. which helps the users to


know it's a constant value. If we encounter any variable
with all capital letters, we should not change their
values by convention, not by rules. Let’s see an
example in the next slide.
CONSTANT IN PROGRAMMING
Example
The pi is the mathematical constant which is
approximately equal to 3.14159. Let’s declare the
constant pi value in python.
# declare constants
PI = 3.14159
print(PI)
Output
3.14
CONSTANT IN PROGRAMMING
Example
We can also write the name in capital letters with underscores
separating words.

# declare constants
LUMINOUS_EFFICACY = 683
VALUE_A = 100 Output
COLOR = 'RED' 683
100
print(LUMINOUS_EFFICACY) RED
print(VALUE_A)
print(COLOR)
CONSTANT IN PROGRAMMING
As we see, the constants are also created exactly as the
variables. Both the variable and constants follow
similar naming rules, the only difference is the
constants use uppercase letters only.
Example
Usually, in Python constants are declared in a module.
Let’s take an example and create constants.
Declare constants in a separate file and name that file
with the .py extension.
CONSTANT IN PROGRAMMING
[Link] file
# declare constants
SPEED_OF_LIGHT_IN_VACUUM = 299792458
PI = 3.141592653589793
LUMINOUS_EFFICACY = 683
VALUE = 20 Output
20
[Link] file 299792458
import Constants 3.141592653589793
print([Link])
print(Constants.SPEED_OF_LIGHT_IN_VACUUM)
print([Link])
SEARCHING ALGORITHM
Searching Algorithms are designed to check for an
element or retrieve an element from any data structure
where it is stored.
The searching algorithms are used to search or find
one or more than one element from a dataset. These
type of algorithms are used to find elements from a
specific data structures.
Searching may be sequential or not. If the data in the
dataset are random, then we need to use sequential
searching. Otherwise we can use other different
techniques to reduce the complexity.
CATEGORIES SEARCHING ALGORITHM
▪Sequential Search: In this, the list or array is
traversed sequentially and every element is
checked. For example: Linear Search,
▪Interval Search: These algorithms are
specifically designed for searching in sorted
data-structures. These type of searching
algorithms are much more efficient than Linear
Search as they repeatedly target the center of
the search structure and divide the search
space in half. For Example: Binary Search.
TYPES SEARCHING ALGORITHM
▪ Binary Search
▪ Exponential Search
▪ Interpolation Search
▪ Jump Search
▪ Linear Search
▪ Ternary Search
LINEAR SEARCH
Linear Search is defined as a sequential search
algorithm that starts at one end and goes through each
element of a list until the desired element is found,
otherwise the search continues till the end of the data
set.
Linear searching techniques are the simplest
technique. In this technique, the items are searched one
by one. This procedure is also applicable for unsorted
data set. Linear search is also known as sequential
search. It is named as linear because its time
complexity is of the order of n O(n).
LINEAR SEARCH
LINEAR SEARCH
Input:
A list of data: 20 4 89 75 10 23 45 69
Find the location for the search key 10
Output:
Item found at location: 4
LINEAR SEARCH
How Does Linear Search Algorithm Work?
In Linear Search Algorithm,
▪ Every element is considered as a potential match for
the key and checked for the same.
▪ If any element is found equal to the key, the search is
successful and the index of that element is returned.
▪ If no element is found equal to the key, the search
yields “No match found”.
LINEAR SEARCH
How Does Linear Search Algorithm Work?

For example: Consider the array arr[] = {10, 50, 30, 70,
80, 20, 90, 40} and key = 30

Step 1: Start from the first element (index 0) and


compare key with each element (arr[i]).
LINEAR SEARCH
How Does Linear Search Algorithm Work?
Comparing key with first element arr[0]. Since not
equal, the iterator moves to the next element as a
potential match.
LINEAR SEARCH
How Does Linear Search Algorithm Work?
Comparing key with next element arr[1]. SInce not
equal, the iterator moves to the next element as a
potential match.
LINEAR SEARCH
How Does Linear Search Algorithm Work?
Step 2: Now when comparing arr[2] with key, the value
matches. So the Linear Search Algorithm will yield a
successful message and return the index of the
element when key is found (here 2).
LINEAR SEARCH

Advantages of Linear Search

▪ Linear search can be used irrespective of whether the


array is sorted or not. It can be used on arrays of any
data type.
▪ Does not require any additional memory.
▪ It is a well-suited algorithm for small datasets.
LINEAR SEARCH

Drawbacks of Linear Search

▪ Linear search has a time complexity of O(N), which in


turn makes it slow for large datasets.
▪Not suitable for large arrays.
LINEAR SEARCH

When to use Linear Search?

▪ When we are dealing with a small dataset.


▪ When you are searching for a dataset stored in
contiguous memory.

You might also like