0% found this document useful (0 votes)
13 views

Chapter 2 - Algorithm Design (1)

The document outlines the essential properties of algorithms, including definiteness, effectiveness, and finiteness, which ensure clarity, feasibility, and termination of algorithms. It also discusses algorithm design, emphasizing the trade-off between time and memory efficiency, and details the steps involved in problem development and algorithm specification. Additionally, it explains the representation of algorithms through pseudocode and flowcharts, along with control structures like sequence, selection, and looping.

Uploaded by

mahdiware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter 2 - Algorithm Design (1)

The document outlines the essential properties of algorithms, including definiteness, effectiveness, and finiteness, which ensure clarity, feasibility, and termination of algorithms. It also discusses algorithm design, emphasizing the trade-off between time and memory efficiency, and details the steps involved in problem development and algorithm specification. Additionally, it explains the representation of algorithms through pseudocode and flowcharts, along with control structures like sequence, selection, and looping.

Uploaded by

mahdiware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Properties of Algorithm

❖ Definiteness:

• Each instruction should be clear and unambiguous.

• It must be perfectly clear what should be done.

• Example Directions which are not permitted

– “add 6 or 7 to x”

– compute x/0

• it is not clear which of the 2 possibilities should be done.

• Achievement using programming language for algorithms

• – designed such that each legitimate sentence has a unique meaning.

❖ 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

– Performing arithmetic on integers

• Example not effective

– Some arithmetic with real numbers.

– 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.

• The time for termination should be reasonably short

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.

• To solve a problem, different approaches can be followed.

• 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 Development Steps

The following steps are involved in solving computational problems.

• Problem definition

• Development of a model

• Specification of an Algorithm

• Designing an Algorithm

• Checking the correctness of an Algorithm

• Analysis of an Algorithm

• Implementation of an Algorithm

• Program testing

• Documentation

An algorithm can be designed or described in natural language or pseudo-code or flow chart


diagrams

12
1. Pseudo - code
Algorithm can be represented in Text mode and Graphic mode

• Graphical representation is called Flowchart

• Text mode most often represented in close to any High level language such as C ,Pascal
Pseudocode

• Pseudocode: High-level description of an algorithm.

✓ More structured than plain English.

✓ Less detailed than a program.

✓ Preferred notation for describing algorithms.

✓ Hides program design issues.

Difference between Algorithm and 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.

For example, following is an algorithm for Insertion Sort.

Algorithm: Insertion-Sort

• Input: A list L of integers of length n

• Output: A sorted list L1 containing those integers present in L

• Step 1: Keep a sorted list L1 which starts off empty

• Step 2: Perform Step 3 for each element in the original list L

13
• Step 3: Insert it into the correct position in the sorted list L1.

• Step 4: Return the sorted list

• Step 5: Stop

Here is a pseudocode

for i ← 1 to length(A)

x ← A[i]

j←i

while j > 0 and A[j-1] > x

A[j] ← A[j-1]

j←j-1

A[j] ← x

Here is a pseudocode Example 2

Algorithm arrayMax(A, n)

✓ Input array A of n integers


✓ Output maximum element of A

Pseudocode

currentMax ← A[0]

for i ← 1 to n-1 do

if A[i] > currentMax then

currentMax ← A[i]

return currentMax
14
Pseudocode Conventions

1. Comments begin with //

2. Blocks are indicated with matching braces { }

3. An identifier begins with a letter

4. Assignment of values to variables is done using the assignment statement

< variable> :=<expression>

5. Logical operators and or not and the relational operators <, ≤, =,≠,>, ≥ are provided

6. The following looping statements are employed:

for, while, repeat-until

7. The following conditional statements can be used

If……. Then….

if …….then……else……

8. Inputs and outputs are done using the instructions read and write

9. There is only one procedure: Algorithm

the heading takes the form Algorithm Name ( parameter list)

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.

It is mainly used to understand the program structure clearly and completely

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.

2. Re-running a certain piece of code again.

3. Skipping a bunch of code.

There are two main types of control structures

1. Sequence

A sequence of instructions in a sequence structure must all be carried out.

17
2. Selection:

In a selection structure certain instructions will only be carried out IF a certain

condition is found to be true.

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

You might also like