Chapter 2 - Algorithm Design (1)
Chapter 2 - Algorithm Design (1)
❖ Definiteness:
– “add 6 or 7 to x”
– compute x/0
❖ Effectiveness
• Each step must be such that it can at least in principle be done by a person using paper and pencil
in a finite amount of time.
• Example –Effective
– if k is the largest integer n such that x^n + y^n = z^n in all positive integers then go to step 4
❖ Finiteness
• The algorithm should terminate after a finite number of steps in all the cases.
11
CHAPTER 2: ALGORITHM DESIGN
• The important aspects of algorithm design include creating an efficient algorithm to solve a
problem in an efficient way using minimum time and space.
• Some of them can be efficient with respect to time consumption, whereas other approaches may
be memory efficient.
• However, one has to keep in mind that both time consumption and memory usage cannot be
optimized simultaneously.
• If we require an algorithm to run in lesser time, we have to invest in more memory and if we
require an algorithm to run with lesser memory, we need to have more time.
• Problem definition
• Development of a model
• Specification of an Algorithm
• Designing an Algorithm
• Analysis of an Algorithm
• Implementation of an Algorithm
• Program testing
• Documentation
12
1. Pseudo - code
Algorithm can be represented in Text mode and Graphic mode
• Text mode most often represented in close to any High level language such as C ,Pascal
Pseudocode
• An algorithm is a formal definition with some specific characteristics that describes a process,
which could be executed by a Turing-complete computer machine to perform a specific task.
• On the other hand, pseudocode is an informal and human readable description of an algorithm
leaving many granular details of it.
• Writing a pseudocode has no restriction of styles and its only objective is to describe the high-
level steps of algorithm in a much realistic manner in natural language.
Algorithm: Insertion-Sort
13
• Step 3: Insert it into the correct position in the sorted list L1.
• Step 5: Stop
Here is a pseudocode
for i ← 1 to length(A)
x ← A[i]
j←i
A[j] ← A[j-1]
j←j-1
A[j] ← x
Algorithm arrayMax(A, n)
Pseudocode
currentMax ← A[0]
for i ← 1 to n-1 do
currentMax ← A[i]
return currentMax
14
Pseudocode Conventions
5. Logical operators and or not and the relational operators <, ≤, =,≠,>, ≥ are provided
If……. Then….
if …….then……else……
8. Inputs and outputs are done using the instructions read and write
15
2. Flowchart
Flowcharts are graphical methods of outlining program structure and generally utilize a set of standard
symbols known as the ANSI (American National Standards Institute) symbols.
Control Structures:
A control structure is a block of programming that analyses variables and chooses a direction in
which to go based on given conditions. The computer program has a strict set of rules to decide which
direction to go. So, this decision that must be made, that will in turn affect the flow of code, is known as
a control structure.
16
Three things will happen because of control structures. They are
1. Control will be moved from one place to other part of the program.
1. Sequence
17
2. Selection:
1. (IF-THEN-ELSE) in this structure, one condition will be checked if it is TRUE then procedure 2
will be followed or else procedure 3 will be followed.
Example
2. (IF-THEN-ELSEIF-THEN-ELSE)
In this structure, first one condition will be checked if it is TRUE then procedure 2 will be followed or
else another one condition will be checked if it true then procedure 3 will be followed or else procedure
4 will be followed
Example:
18
3. Looping Structures
Loop structures allow you to run one or more lines of code repetitively. You can repeat the
statements in a loop structure until a condition is True, until a condition is False, a specified number of
times, or once for each element in a collection. Running a set of statements until a condition becomes
true
An infinite loop is one that lacks a functioning exit routine. The result is that the loop repeats
continually until the operating system senses it and terminates the program with an error or until some
other event occurs.
19
It provides the following types of loops to handle the looping requirements:
1. While Loop
2. For Loop
3. Do ... While Loop
1. While Loop
• While loop allows to repeatedly run the same block of code, until a given condition
becomes true.
• It is called an entry-controlled loop statement and used for repetitive execution of the
statements.
• The loop iterates while the condition is true. If the condition becomes false, the program
control passes to the next line of the code.
20