You are on page 1of 17

Algorithms

23-Sep-19
Agenda…

Muzaffar Iqbal Farooqi


- How to Write Algorithms
- Types of Algorithms

1
How to Write Algorithms
• There are the building blocks of an algorithm.
- Sequence

23-Sep-19
- Decision
- Repetition

Muzaffar Iqbal Farooqi


2
How to Write Algorithms…

Muzaffar Iqbal Farooqi 23-Sep-19


3
Properties of Algorithm
 Input:
 An algorithm has zero or more inputs, taken from a

23-Sep-19
specified set of objects.
 Output:
 An algorithm has one or more outputs, which have a

Muzaffar Iqbal Farooqi


specified relation to the inputs.
 Finiteness:
 terminates after a finite number of steps
 Definiteness:
 Each step must be precisely defined; the actions to be
carried out must be unambiguous.
 Effectiveness:
 steps must be simple and basic that they can be done
exactly and in finite length. 4
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM double(n)
// The purpose of this algorithm is to double a positive integer

Muzaffar Iqbal Farooqi


while n >= 0 do
n  2n

5
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM double(n)
// The purpose of this algorithm is to double a positive integer

Muzaffar Iqbal Farooqi


while n >= 0 do
n  2n

Input: Yes, since n is a positive number,


Finiteness: No, the while loop in this procedure will run forever,
therefore this procedure is not finite.

Modify above procedure so that they satisfies all the properties


ALGORITHM double(n)
n  2n 6
return n
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM sum(n)
// The purpose of this algorithm is to find the sum of n integer.

Muzaffar Iqbal Farooqi


sum  0
while i <= n do
sum  sum + i
i  i+1
return sum

7
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM sum(n)
// The purpose of this algorithm is to find the sum of n integer.

Muzaffar Iqbal Farooqi


sum  0
while i <= n do
sum  sum + i
i  i+1
return sum

The value of i is never set in the algorithm, so the given


algorithm lacks definiteness. Without knowing the initial 8
value of i, the behavior of this algorithm is undetermined.
Properties of Algorithm…
 The correct algorithm is as below…

23-Sep-19
ALGORITHM sum(n)
// The purpose of this algorithm is to find the sum of n integer.

Muzaffar Iqbal Farooqi


sum  0
n  1
while i <= n do
sum  sum + i
i  i+1
return sum

9
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM divide(a,b)
// The purpose of this algorithm is to divide the first value by the

Muzaffar Iqbal Farooqi


second value
c  a/b
return c

10
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM divide(a,b)
// The purpose of this algorithm is to divide the first value by the

Muzaffar Iqbal Farooqi


second value
c  a/b
return c

 Procedure is not effective since the line “c  a/b” cannot be


executed if b=0.

11
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM largest(a,b)
// The purpose of this algorithm is to return the largest value

Muzaffar Iqbal Farooqi


if a > b
return a
if b > a
return b

12
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM largest(a,b)
// The purpose of this algorithm is to return the largest value

Muzaffar Iqbal Farooqi


if a > b
return a
if b > a
return b

 The procedure is not complete. What will be returned if both


a and b are equal values.
13
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM largest(a,b)
// The purpose of this algorithm is to return the largest value

Muzaffar Iqbal Farooqi


if a > b
return a
else
return b

14
Properties of Algorithm…
 Determine which characteristics of algorithms is
missing in the following…

23-Sep-19
ALGORITHM largest(a,b)
// The purpose of this algorithm is to return the largest value

Muzaffar Iqbal Farooqi


if a > b
return a
else
return b

 The procedure is ambiguous.

15
Test
 Is the following a legitimate algorithm…
ALGORITHM test

23-Sep-19
// The purpose of this algorithm is to display integer values from
1 to 10

Muzaffar Iqbal Farooqi


a1
while a <= 10
print a
aa+1

16
Test
 Is the following a legitimate algorithm…
ALGORITHM test

23-Sep-19
// The purpose of this algorithm is to display integer values from
1 to 10

Muzaffar Iqbal Farooqi


a1
while a <= 10
print a
aa+1

 The procedure is ambiguous.

17

You might also like