Algorithms
Algorithms
OBJECTIVES
Understand the concept of an algorithm.
2
Introduction to Algorithms
An algorithm is a plan, a logical step-by-step
process for solving a problem normally
expressed using flowchart or pseudocode or
Natural Languages.
The key to any problem solving task is to guide
your thought process.
The most useful thing to do is keep asking ‘What
if we did it this way?’ Exploring different ways of
solving a problem can help come up with more3
Introduction to Algorithms cont
Therefore, when designing an algorithm,
consider if there is more than one way of
solving the same problem.
Understanding the problem is very important:
Before designing an algorithm, it is important to
check and completely understood the problem
you are solving.
4
Basic things to help in
problem understanding
The inputs: What will be the inputs into the
problem?
The outputs: What will be the outputs of the
problem?
Operation: In what order do instructions need to
be carried out?
Decision: What decisions need to be made in the
problem?
Recursion: Are any areas of the problem 5
Designing an algorithm
The algorithm of a program can be designed
using pseudo code or a flowchart
6
Designing an algorithm using
pseudo code.
Pseudo code is a way of expressing an
algorithm in structured English that resembles
computer programming language.
It does not enforce strict syntax and therefore,
7
Designing an algorithm using
pseudo code cont...
Pseudo code uses commands, keywords and
structures similar to those found in programming
languages.
Cannot be understood by computers, but is used
to develop the logic of a program without
needing to worry about the syntax.
A solution in pseudo code can then be
converted into a high-level language.
Uses English-like phrases to outline the task.
8
Designing an algorithm using
Natural language.
Natural language comprises of set of steps
written in a language that can be understood by
human. Such languages are English.
The steps are listed and explained in a
sequential order.
9
Designing an algorithm using
Natural language.
Natural language comprises of set of steps
written in a language that can be understood by
human. Such languages are English.
The steps are listed and explained in a
sequential order.
10
Designing an algorithm using
Flowcharts.
Flowcharts are diagrammatic/schematic
representations of processes taken to solve a problem.
Flowcharts uses notation to represent the start point,
end points, inputs, outputs, possible paths and
the decisions that lead to these possible paths.
Flow-charts can be created by hand or manually in
most office software, but lately specialized diagram
drawing software has emerged that can also be used
for the purpose.
11
Flowcharts
• Graphically depict the logical steps to
carry out a task and show how the steps
relate to each other.
12
Flowchart Symbols (Mostly Used)
• Processing steps
• Input/Output
• Conditional (Decision)
14
Flowchart symbols continued
15
Flowchart
example
16
Statement structures
• Sequence – follow instructions from one line to the
next without skipping over any lines
• Decision - if the answer to a question is “Yes” then
one group of instructions is executed. If the answer
is “No,” then another is executed
• Looping – a series of instructions are executed over
and over
17
Divide-and-conquer method
• Used in problem solving – take a large
problem and break it into smaller
problems solving the small ones first
• Breaks a problem down into modules
18
Sequence
flow chart
19
Direction of Numbered Nairobi
Streets Algorithm
• Problem: Given a street number of a one-way
street in Nairobi City, decide the direction of
the street, either eastbound or westbound
• Discussion: In Nairobi City even numbered
streets are Eastbound, odd numbered streets
are Westbound
20
Flowchart
21
Pseudocode
End
END
An Algorithm in Pseudocode: Example 2
Start
SEND ‘Enter age’ TO DISPLAY
Enter age RECEIVE age FROM (INTEGER)
Enter your age KEYBOARD
If the age is larger NO
type =
than 18 set the type If age > 17
child IF age > 17 THEN
to adult Output SET type TO adult
YES
Else if the age is less ‘child’ ELSE
type = adult
than or equal to 18 set End SET type TO child
the type to child Output ‘adult’
END IF
Display the type
End SEND type TO DISPLAY
END