Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

HND in Computing and Software

Engineering
SEC5213: Data Structures and Algorithm
Level: 5
Credit Value: 20

Lesson 02 – Introduction to Algorithms

1
Lecturer: Ms. Sathananthy 06/04/2022
Learning Outcome 02

Understand the fundamentals of various common algorithms

2 06/04/2022
Outline

 Fundamentals of Algorithms

- Write algorithms to solve simple problems

 Miscellaneous problem solving

 Performance of an algorithm

 Characteristics of an algorithm
7
Algorithm

An algorithm is a procedure having well defined steps for solving a

particular problem. Algorithm is finite set of logic or instructions, written


in order for accomplish the certain predefined task.

It is not the complete program or code, it is just a solution (logic) of a

problem, which can be represented either as an informal description using


a Flowchart or Pseudo code.

4 06/04/2022
Algorithm – problem solving

Example 1: Calculate the Interest of a Bank Deposit


Algorithm:
Step 1: Read amount,
Step 2: Read years,
Step 3: Read rate,
Step 4: Calculate the interest with formula
"Interest=Amount*Years*Rate/100
Step 5: Print interest,

5 06/04/2022
Algorithm – problem solving

Example 2: Determine and Output Whether Number N is Even or


Odd

Algorithm:
Step 1: Read number N,
Step 2: Set remainder as N modulo 2,
Step 3: If remainder is equal to 0 then number N is even, else
number N is odd,
Step 4: Print output

6 06/04/2022
Algorithm – problem solving

 Example 3: Determine whether a temperature


is Below or Above the Freezing Point

Algorithm:

Step 1: Input temperature,

Step 2: If it is less than 32, then print "below

freezing point", otherwise print "above freezing

point

7 Step 3: Print output 06/04/2022


Algorithm – problem solving
 Example 4: log in to Facebook account

Algorithm:
Step 1: Enter www.facebook.com in your browser.  (I/O)

Step 2: Facebook Home page loads   (PROCESS)

Step 3: Enter your Email ID and Password    (I/O)

Step 4: Is Email ID and Password Valid (DECISION)


if NO then
Log in error  (PROCESS)
go to step 3
else
Display Facebook Account (I/O)
Stop
8 06/04/2022
Miscellaneous problems solving

1. Draw four line segments through the 9 points without lifting your pencil or
retracing a line

9 06/04/2022
Miscellaneous problems solving

1. Do You Owe Me Money?

There are three brothers named John, James and William, who look exactly alike. John

and James always lie, but William always tells the truth. You meet one of the brothers

on the street one day, and want to know if he is John (because John owes you money).

You are allowed to ask ONE question that can be answered “yes” or “no”, but the

question may not contain more than 3 words. What question would you ask?
10 06/04/2022
Solution
The questions would be, “Are you James?”(The goal is to find John, the one that

owes you money?)


If the person is James, he will answer “NO”(because he lies)

If the person is John, he will say “Yes”(he owes you money and he lies)

If the person is William, he will say “No”(he tells the truth)

So, if the answer is “yes” you know that it is John and you can then demand your

money.
You have a 50% probability of knowing if it is James with this question.

11 06/04/2022
Miscellaneous problems solving

 Three men are condemned to die, one of whom is blind. The king decides he will offer them an

opportunity to be set free. The three men are arranged in a circle facing one another. The king

produces 5 hats: 2 black and 3 white. The king places a hat on the head of each person and then

destroys the two remaining hats. The men have no idea which hats have been destroyed. The

king instructs them, “The first one of you who can tell me the color of his hat will be set free.” A

period of time passes in silence and then finally the blind man tells the king the color of his hat

and is set free. What color hat was the blind man wearing and how did he know?

12 06/04/2022
Characteristics of an Algorithm

An algorithm must follow the mentioned below characteristics:


 Input: An algorithm must have 0 or well defined inputs.

 Output: An algorithm must have 1 or well defined outputs, and should match with the

desired output.
 Feasibility: An algorithm must be terminated after the finite number of steps.

 Independent: An algorithm must have step-by-step directions which is independent of

any programming code.


 Unambiguous: An algorithm must be unambiguous and clear. Each of their steps and

input/outputs must be clear and lead to only one meaning.


13 06/04/2022
Performance of Algorithm
The performance of algorithm is measured on the basis of following properties:
i. Time complexity: It is a way of representing the amount of time needed by a program to run
to the completion.
ii. Space complexity: It is the amount of memory space required by an algorithm, during a
course of its execution. Space complexity is required in situations when limited memory is
available and for the multi user system.

Each algorithm must have:


iii. Specification: Description of the computational procedure.
iv. Pre-conditions: The condition(s) on input.
v. Body of the Algorithm: A sequence of clear and unambiguous instructions.
14 vi. Post-conditions: The condition(s) on output.
Example: Design an algorithm to multiply the two numbers x and y and display the result in
z

Example: Design an algorithm to multiply the Alternatively the algorithm can be written


two numbers x and y and display the result in z.
as ?
Step 1 START
Step 1 START MULTIPLY
Step 2 declare three integers x, y & z
Step 2 get values of x & y
Step 3 define values of x & y

Step 4 multiply values of x & y Step 3 z← x * y

Step 5 store the output of step 4 in z Step 4 display z


Step 6 print z Step 5 STOP
Step 7 STOP
15 06/04/2022
Algorithms – contd.

Commonly used algorithms include are useful for:

Searching for a particular item

Sorting the data

Iterating through all the items in a data structure

16
Next – Asymptotic Analysis

17 06/04/2022

You might also like