Day1 Algorithms
Day1 Algorithms
1
2
What is an algorithm?
An algorithm is “a finite set of precise
instructions for performing a computation
or for solving a problem”
A program is one type of algorithm
All programs are algorithms
Not all algorithms are programs!
7
What is an pseudocode?
Pseudocode is an artificial and informal
language that helps programmers to
develop algorithms.
Pseudocode is not a programming language, it is a simple
way of describing a set of instructions that does not have to
use specific syntax.
Writing in pseudocode is similar to writing in a
programming language. Each step of the algorithm is
written on a line of its own in sequence.
Usually, instructions are written in
uppercase, variables in lowercase and messages in
sentence case.
8
Example1 of pseudocode
A simple program could be created to ask someone their name and
age, and to make a comment based on these. This program
represented in pseudocode would look like this:
START
OUTPUT 'What is your name?'
INPUT user inputs their name
STORE the user's input in the name variable
OUTPUT 'Hello' + name
OUTPUT 'How old are you?'
INPUT user inputs their age
STORE the user's input in the age variable
IF age >= 70 THEN
OUTPUT 'You are aged to perfection!'
ELSE
OUTPUT 'You are a spring chicken!‘
END
9
Example2 of pseudocode
Problem - Design an algorithm to add two
numbers and display the result.
Step 1 – START
Step 2 - declare three integers a, b & c
Step 3 - define values of a & b
Step 4 - add values of a & b
Step 5 - store output of step 4 to c
Step 6 - print c
Step 7 - STOP
10
Flowcharts
11
Common basic flowchart symbols
These symbols are
used to construct
the flowchart and
show the step-by-
step solution to
the problem.
12
Example of flowchart
Planning a program that asks people what
the best subject they take is, would look
like this as a flowchart:
13
Algorithm Analysis
Algorithm Analysis Efficiency of an algorithm can be
analyzed at two different stages, before implementation and
after implementation. They are the following –
A Priori Analysis - This is a theoretical analysis of an
algorithm. Efficiency of an algorithm is measured by
assuming that all other factors, for example, processor speed,
are constant and have no effect on the implementation.
A Posterior Analysis - This is an empirical analysis of an
algorithm. The selected algorithm is implemented using
programming language. This is then executed on target
computer machine. In this analysis, actual statistics like
running time and space required, are collected.
14
Algorithm Efficiency
Two areas are important for
performance:
space efficiency - the memory required,
also called, space complexity
time efficiency - the time required, also
called time complexity
15
Some algorithms are more efficient than others. We would prefer to
chose an efficient algorithm, so it would be nice to have metrics for
comparing algorithm efficiency.
The complexity of an algorithm is a function describing the efficiency of
the algorithm in terms of the amount of data the algorithm must process.
Usually there are natural units for the domain and range of this function.
There are two main complexity measures of the efficiency of an algorithm:
Time complexity is a function describing the amount of time an
algorithm takes in terms of the amount of input to the algorithm. "Time"
can mean the number of memory accesses performed, the number of
comparisons between integers, the number of times some inner loop is
executed, or some other natural unit related to the amount of real time the
algorithm will take.
Space complexity is a function describing the amount of memory (space)
an algorithm takes in terms of the amount of input to the algorithm. We
often speak of "extra" memory needed, not counting the memory needed
to store the input itself. Again, we use natural (but fixed-length) units to
measure this. We can use bytes, but it's easier to use, say, number of
integers used, number of fixed-sized structures, etc. In the end, the
function we come up with will be independent of the actual number of
16
bytes needed to represent the unit.
Questions
17
quiz
What is time complexity of algorithm
Identify 6 characteristics/ properties of
algorithm
What are difference between priori and
posterior analysis of algorithm?
Write a pseudocode and flowchart which get
age of use and decide If he has ID or not ( age
of get ID is 16 and above).
18