Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
167 views

Algorithms and Programming Concepts

Uploaded by

Whatsapp stuts
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views

Algorithms and Programming Concepts

Uploaded by

Whatsapp stuts
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 160

ALGORITHMS

AND
PROGRAMMING CONCEPTS

(FOR PRIVATE CIRCULATION ONLY)

2019
PROGRAMME COORDINATOR
Prof. Sonali Karale

COURSE DESIGN AND REVIEW COMMITTEE


Prof. Nidhi Khare Ms. Prerna Patil
Prof. Atul Kahate Prof. Sonali Karale
Ms. Apoorva Kulkarni Ms. Minal Abhyankar

COURSE WRITERS
Prof. Vidya Kumbhar Prof. Sonal Khosla
Ms. Prerna Patil Prof. Nidhi Khare

EDITOR
Ms. Neha Mule

Published by Symbiosis Centre for Distance Learning (SCDL), Pune


July, 2014 (Revision 01, 2019)

Copyright © 2019 Symbiosis Open Education Society


All rights reserved. No part of this book may be reproduced, transmitted or utilised in any form or
by any means, electronic or mechanical, including photocopying, recording or by any information
storage or retrieval system without written permission from the publisher.

Acknowledgement
Every attempt has been made to trace the copyright holders of materials reproduced in this book.
Should any infringement have occurred, SCDL apologises for the same and will be pleased to make
necessary corrections in future editions of this book.
PREFACE

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations.


Algorithms are used for calculation, data processing, and automated reasoning.

An algorithm is an effective method expressed as a finite list of well-defined instructions for calculating
a function. Starting from an initial state and initial input, the instructions describe a computation
that, when executed, proceeds through a finite number of well-defined successive states, eventually
producing “output” and terminating at a final ending state. The transition from one state to the next is
not necessarily deterministic; some algorithms, known as randomised algorithms, incorporate random
input. A sorting algorithm is an algorithm that puts elements of a list in a certain order. A search
algorithm is an algorithm for finding an item with specified properties among a collection of items.
The items may be stored individually as records in a database.

Once you go through the various methods of writing algorithms given in this SLM, you will be able to
apply them in any programming languages. We have also discussed special problems and algorithms
which will help the learners to understand and implement the concepts studied in the earlier units.

Prof. Vidya Kumbhar


Prof. Sonal Khosla
Prof. Prerna Patil
Prof. Nidhi Khare

iii
ABOUT THE AUTHORS

Prof. Vidya Kumbhar has completed her Master of Computer Management (MCM) and Master of
Computer Applications (MCA). She has more than 10 years of teaching experience for various man-
agement institutes in Information Technology domain.

She is also associated with different institutes for various educational related assignments. She has
worked on various software development projects in National Informatics Centre, Pune and is cur-
rently working on minor research projects of Symbiosis Institute of Research and Innovation. She is
also working as Assistant Professor, and pursuing her PhD from Symbiosis International University,
Pune. She had published various research papers in National and International Journals and confer-
ences.
Prof. Sonal Khosla has completed her Master in Computer Application (MCA) from Maharishi
Dayanand University, Rohtak.  She has nine plus years of teaching experience and is currently work-
ing with Symbiosis Institute of Computer Studies and Research, Pune, as Assistant Professor. Before
joining SICSR, she has worked with Ansal Institute of Technology, Gurgaon.
Her areas of specialisations are Database Management, Computer Networks, Algorithm Designing
and Programming. She also has various research papers to her credit in various national and interna-
tional journals and conferences.
Prof. Prerna Patil has completed her Master in Computer Science from Pune University. She has
nine plus years of software development/IT experience. Furthermore, she has rich experience in
Academics and Training. She has published several articles and has won the Trainer of the Year’
2010 award at Oracle Financial Service Software Limited. Her areas of specialization include cloud
computing, trends in software development and agile methodology.

Prof. Nidhi Khare has a rich experience of 13 years in Academics, Industry as well as Corporate
Training. She has also headed the Computer Department of a college during her Academics career.
Along with Masters in Computer Applications, she has Certifications in Advanced Computing and
Office Automation to her credit. She has published eight papers in National and International con-
ferences. She was the Faculty of Information Technology at SCDL.

iv
CONTENTS

Unit No. TITLE Page No.


1 Pseudocodes 1-12
1.1 Introduction
1.2 Writing Pseudocodes
1.3 Rules for writing Pseudocodes
1.4 Writing Different Statements in Pseudocode
1.4.1 Sequence Statements in Pseudocode
1.4.2 Selection Statements in Pseudocode
1.4.3 Repetition Statements in Pseudocode
1.5 Advantages and Disadvantages of Pseudocode
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

2 Algorithms 13-36
2.1 Introduction
2.2 Basics of Algorithm
2.3 Benefits of Algorithm
2.4 Constructs for developing Algorithm
2.4.1 Sequence
2.4.2 Decision
2.4.3 Repetition
2.5 Tools to represent an Algorithm
2.5.1 Flowchart
2.6 Concept of Modularity and Sub-Algorithms
2.7 Concept of Object Oriented Programming
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

v
Unit No. TITLE Page No.
3 Designing and Analysing Algorithms 37-54
3.1 Introduction
3.2 Desirable Properties of an Efficient Algorithm
3.3 Computational Complexity of Algorithms
3.4 Asymptotic Notations
3.5 Different Analysis of Algorithms
3.6 Example Analysis
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading
4 Writing Language-Neutral Algorithms 55-66
4.1 Introduction
4.2 Need for Language-Neutral Algorithms
4.3 Components of an Algorithm
4.4 Standard Format for writing an Algorithm
4.4.1 Predefined Keywords for an Algorithm
4.5 Organisation of an Algorithm
4.6 Verifying if Algorithm is Language-Neutral
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

vi
Unit No. TITLE Page No.
5 Algorithms for Programming Cases 67-88
5.1 Introduction
5.2 Sequential Flow
5.3 Random Jump in the Flow
5.4 Conditional Flow
5.5 Repetitive Flow
5.6 Iterative Flow
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

6 Advanced Constructs using Algorithms 89-108


6.1 Introduction
6.2 Nested Sequences
6.3 Parallel Execution
6.4 Fork and Join
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

7 Searching Algorithms 109-118


7.1 Introduction
7.2 Searching Algorithms
7.3 Linear Search
7.3.1 Complexity of Linear Search
7.4 Binary Search
7.4.1 Complexity of Binary Search
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

vii
Unit No. TITLE Page No.

8 Sorting Algorithms 119-136


8.1 Introduction
8.2 Types of Sorting Algorithms
8.3 Selection Sort
8.4 Bubble Sort
8.5 Merge Sort
8.6 Quick Sort
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

9 Special Problems and Algorithms 137-152


9.1 Introduction
9.2 Divide and Conquer Algorithms
9.3 Dynamic Programming
9.4 Greedy Algorithms
9.5 P and NP Complete Problems
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

viii
Pseudocodes
UNIT

1
Structure:

1.1 Introduction
1.2 Writing Pseudocodes
1.3 Rules for writing Pseudocodes
1.4 Writing Different Statements in Pseudocode
1.4.1 Sequence Statements in Pseudocode
1.4.2 Selection Statements in Pseudocode
1.4.3 Repetition Statements in Pseudocode
1.5 Advantages and Disadvantages of Pseudocode
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Pseudocodes 1
Notes
Objectives
----------------------
After going through this unit, you will be able to:
----------------------
l Discuss imitation of the code before writing the actual one
----------------------
l Explain the structures available in various programming languages
---------------------- l Analyse the logic for writing computer programs
----------------------

---------------------- 1.1 INTRODUCTION


---------------------- Most often a pseudocode is required to write a rough draft of the code
before writing the actual program. Pseudocode is a detailed description
----------------------
of an algorithm or computer program with more focus on readability and
---------------------- understandability and less focus on technical requirements. It is written in a
formally styled natural language. It can be best described as a combination of
---------------------- code and English language.
---------------------- The advantage of writing pseudocodes is that it provides the advantages
of an informal language like English along with the precision of code. This high
---------------------- level description helps the programmer to easily transform it into a code, since
---------------------- it mimics the actual code. It is more focused on writing the logic of the program
rather than the code.
----------------------
1.2 WRITING PSEUDOCODES
----------------------
A computer program can perform the following list of operations like:
----------------------
• Receive information
----------------------
• Output information
---------------------- • Perform various operations
---------------------- • Assign values
---------------------- • Compare two or more options and select one
• Repeat a group of steps
----------------------
For all the above operations, pseudocode can be written.
----------------------
For receiving or inputting information in a computer program the “get”,
---------------------- “read” and “input” words can be used. The input can be taken from any source
including keyboard, disk or any other device. Any value taken as input from the
---------------------- user needs to be stored somewhere which is generally a variable. For example,
---------------------- to input the marks of a student we write:

---------------------- Read marks

---------------------- where marks is a variable that stores the value entered by the user.

2 Algorithms and Programming Concepts


For displaying the output of a computer program, “display”, “print”, “output”, Notes
“put” or “write” words can be used. The value contained in the variable is printed
on the respective device, which could again be any output device including ----------------------
monitor, file or disk.
----------------------
For example, to display the value stored in the variable marks, we can
write: ----------------------
Print marks ----------------------

----------------------
Display marks
----------------------
Write marks
----------------------
This statement prints the value stored in the variable marks. The value can ----------------------
be entered by the user or assigned to the variable.
To assign some value to a variable, a simple assignment statement can be written ----------------------
as follows: ----------------------
a=5 ----------------------
where the value at the right-hand side of the equal to operator is assigned to the ----------------------
variable a.
----------------------
The right-hand side can also contain an expression, which is computed
and assigned to the left-hand side. ----------------------
For example,
----------------------
a = 5*2
----------------------
So the result of computing 5*2 is assigned to the variable a.
----------------------
“Calculate” and “compute” words can be used for any kind of arithmetic
operations. These operations can be specified using words or mathematical ----------------------
symbols. The basic operators are also used, since pseudocode is a mimic of the
----------------------
actual code.
The list of various mathematical operators that can be used in a pseudocode ----------------------
are as follows. Also the order of operation of any operation in any expression is ----------------------
given from top to bottom.
Table 1.1 Mathematical Operators used in Pseudocode ----------------------

Operator Function
----------------------

() Parenthesis ----------------------

^ or ^^ Exponentiation ----------------------
- Negation ----------------------
*, / Multiplication, Division ----------------------

Pseudocodes 3
Notes % Modulus or Remainder
---------------------- +, - Addition, Subtraction

---------------------- = Assignment

---------------------- For example,

---------------------- Calculate si = (principal*rate*time)/100



----------------------
calculates the right-hand side of the expression and stores them in the variable
---------------------- on the left-hand side.
---------------------- For an expression a = 8 + 7 *20 % 4 ^ 2 - (2 + 3), the order of computation is
as follows:
----------------------
1. A = 8 + 7 * 20% 4 ^ 2 -(2 + 3)
---------------------- 2. A = 8 + 7 * 20 % 4 ^ 2 -(5)
---------------------- 3. A = 8 + 7 * 20 %16 -(5) ( 5 as a negative value)
---------------------- 4. A = 8 + 7 * 20 % 16 - 5
5. A = 8 + 7* 20 % 16 - 5
----------------------
6. A = 8 + 7*1- 5
----------------------
7. A = 8+7 - 5
---------------------- 8. A = 15-5
---------------------- 9. A=10
----------------------
Check your Progress 1
----------------------

---------------------- Multiple Choice Single Response.


1. After a programmer plans the logic of a program, he will next:
----------------------
i. Understand the problem
---------------------- ii. Test the program
---------------------- iii. Translate the program

---------------------- iv. Code the program


2. Which of the following is an assignment operator?
----------------------
i. ==
---------------------- ii. *
---------------------- iii. ^
iv. %
----------------------

----------------------

4 Algorithms and Programming Concepts


State True or False. Notes
1. Pseudocode can be described using only two control structures: ----------------------
sequence and repetition.
----------------------
2. Pseudocode must specify a list of steps to be executed with the
right order. ----------------------
3. Pseudocode can be represented using programming language or ----------------------
flowchart.
4. Algorithm must be very specific and correct. ----------------------

5. Pseudocodes are written in simple English. ----------------------

----------------------

Activity 1 ----------------------

----------------------
Find the value of x.
----------------------
x = 2 + 3 *2 / 1 ^ 2 - (2 + 3)
----------------------

----------------------
1.3 RULES FOR WRITING PSEUDOCODES
----------------------
Although pseudocode is written in an informal language, it is still
necessary to follow certain rules and conventions to make it mimic the code. ----------------------
Following are certain points to be kept in mind to write pseudocode:
----------------------
●● It should be able to convey the intention of the program.
----------------------
●● The statements should be simple and unambiguous.
●● Since it is a balance between code and informal natural language, it is ----------------------
necessary to follow the rules of both.
----------------------
●● Precise and comprehensible English sentences should be written instead
of full sentences. ----------------------
●● Comments should be included where useful and required for better ----------------------
understanding of the code.
●● Variable names should be written as mnemonics, so that it is easy to ----------------------
remember. They are also called identifiers. An identifier should not ----------------------
contain spaces.
●● Although the syntax is not important, it is better to use conventional ----------------------
programming statements like begin/end. ----------------------
●● Programming shorthand should be used instead of using equivalent
English sentences for the same, e.g., a group of statements can be repeated ----------------------
using while statement. ----------------------
●● For better readability, use one statement per line and should represent one
operation to be performed. ----------------------

Pseudocodes 5
Notes ●● Keywords should be written in capital letters, e.g., READ, GET, etc.
●● If hierarchical statements are used, then indent the statements.
----------------------
●● The code should be as far as possible written in a language-independent
---------------------- form.

---------------------- ●● Variable names should be unique to a program.


●● Scope terminators like START and END should be used at the start and
---------------------- end of the pseudocode.
----------------------
1.4 WRITING DIFFERENT STATEMENTS IN
---------------------- PSEUDOCODE
---------------------- The steps of writing the sequence, selection and repetition statements in
---------------------- pseudocodes are discussed below.
1.4.1 Sequence Statements in Pseudocode
----------------------
Sequence construct is a list of statements that are executed sequentially
---------------------- moving from one step to another. Each action is performed one after the other
from top to bottom and left to right as written. The statements are executed
----------------------
without any condition checking. A sequence construct can be a part of selection
---------------------- and repetition statements as well. For example, the sequence of statements
performed to find the simple interest can be written as follows:
----------------------
Read principal
----------------------
Read rate
----------------------
Read time
----------------------
Compute si = (principal*rate*time)/100
---------------------- Print si
---------------------- All the statements in the above pseudocode for computing simple interest
---------------------- are executed one after the other in sequence in the order in which they are
written.
---------------------- Instead of reading the values of principal, rate and time in 3 separate statements,
---------------------- a single statement can also be written. For example,

---------------------- Read principal, rate, time

---------------------- It means the values entered by the keyboard go into the three variables.
The first value entered goes into principal, the second value goes into rate and
---------------------- the third value goes into time.
---------------------- The statement

---------------------- Print si

---------------------- prints the value contained in the variable si to the output screen.

6 Algorithms and Programming Concepts


1.4.2 Selection Statements in Pseudocode Notes
Most often it is required in a code to compare one value with another for
----------------------
making selection between different choices. Thus, sequence allows selecting a
particular option from a given set of choices. ----------------------
For making comparisons, relational operators are used. Table 1.2 shows
----------------------
the list of relational operators used to make comparisons.
Table 1.2 Relational Operators to make Comparisons ----------------------

Symbol Function performed ----------------------


< strictly less than ----------------------
<= less than or equal to
----------------------
> strictly greater than
----------------------
>= greater than or equal to
----------------------
== equal to
!= not equal to ----------------------

Comparisons done using relational operators evaluate to either true or ----------------------


false. These are also called Boolean expressions since they return in a Boolean ----------------------
value. A Boolean expression always consist of two alternatives.
Assuming there are two variables x and y containing the values 4 and 5 ----------------------
respectively, some of the comparison statements are given as below: ----------------------
Comparison Expression Result
----------------------
x + 1 == y true
----------------------
y – 1 == x false
x!=y true ----------------------

The comparison for the given values is done using the If-End If construct. ----------------------
It allows the branching of code with the evaluation of the Boolean expression.
If the Boolean expression returns true, then the set of statements associated ----------------------
with the true branch is executed. Otherwise, the set of statement with the false ----------------------
branch is executed (if given).
For example, pseudcode to check whether a given number is odd or even. ----------------------

Read number ----------------------

d=number%2 ----------------------
If d==0 ----------------------
Print “Number is even” ----------------------
End if
----------------------

----------------------

Pseudocodes 7
Notes In the above pseudocode, a number is taken as input from the user and
stored in the variable number. The value of the number is divided by 2 and the
---------------------- remainder is stored in the variable d.
---------------------- Now the decision or selection control statement checks whether the value
of the remainder is 0 or not. If it is 0, the print statement is executed and prints
---------------------- “Number is even” on the output screen. If it is not equal to 0, it terminates the
code. Selection statement allows the programmer to compare the value of d
----------------------
with 0 and then make a decision.
---------------------- Sometimes, it is required to print another message if the comparison is
not true. In this situation if-Else-End if construct is used.
----------------------
Read number
----------------------
d=number%2
----------------------
If d==0
----------------------
Print “Number is even”
----------------------
else
---------------------- Print “Number is odd”
---------------------- End if
---------------------- Boolean expressions can be made more complex with the use of logical
operators. The different logical operators that are used in a Boolean expression
----------------------
are given in Table 1.3.
---------------------- Table 1.3 Logical Operators used in a Boolean Expression
---------------------- Logical Operator Function

----------------------
AND Returns true when both the conditions
---------------------- (Boolean expressions) are true
OR Returns true if either condition (Boolean expression) is true
----------------------
NOT Returns true if the condition (Boolean expression) is false
----------------------
Suppose there is a situation where a decision has to be made based on
---------------------- the condition if a given number is even and also a multiple of seven. In such
a scenario, the AND operator can be used. It will check both the conditions of
----------------------
even number and multiple of seven.
----------------------
Read number
----------------------
If number %2==0 AND number %7==0 then
---------------------- Print “ Even and multiple of seven”
---------------------- End if

---------------------- In the above example, two Boolean expressions are given

8 Algorithms and Programming Concepts


number %2==0 Notes
and
----------------------
number %7==0
----------------------
Now when both the expressions are true the statement with the true branch is
executed. ----------------------
1.4.3 Repetition Statements in Pseudocode ----------------------
Repetition statements are also known as looping or iteration statements. It is
required that a certain piece of code is repeated in a pseudocode. Since writing ----------------------
the same code again and again brings a lot of confusion and makes a lengthy ----------------------
code, these structures are used. Repetition of this piece of code is also evaluated
through a Boolean expression. It allows the code to be repeated till a decision ----------------------
is made. Following are the structures that are used to implement repetition of a
block of code: ----------------------

●● While ----------------------
●● Do-while ----------------------
●● For loop
----------------------
So with the help of sequence/selection/repetition statements a complex logic
can be created to handle a variety of decision scenarios. ----------------------

----------------------
Check your Progress 2
----------------------
Fill in the blanks.
----------------------
1. _________ is another name for a loop structure.
----------------------
State True or False.
1. Logical operator AND and OR are used in if statement only. ----------------------

2. Comparison or relational operators = = and < are used in if statement ----------------------


only.
----------------------
3. Logical and comparison operators are used in only selection condition.
----------------------
Multiple Choice Single Response.
1. Before the user’s selection enters the selection structure for the loop, ----------------------
it should be:
----------------------
i. Validated
----------------------
ii. Implemented
iii. Discarded ----------------------

iv. Ordered ----------------------

----------------------

----------------------

Pseudocodes 9
Notes
Activity 2
----------------------

---------------------- Write a pseudocode to calculate the radius and circumference of a circle.

----------------------

---------------------- 1.5 ADVANTAGES AND DISADVANTAGES OF


PSEUDOCODE
----------------------
After discussing the concepts of pseudocodes, let us talk about their advantages
---------------------- and disadvantages.
---------------------- Advantages
---------------------- ●● It provides the programmer with a detailed template of the actual code.
●● A dry run of the pseudocode or debugging helps the programmer to
----------------------
find errors easily and is less costly as compared to finding errors in the
---------------------- computer program.
●● If the pseudocode is accepted, it is rewritten using the syntax and
----------------------
conventions of a programming language.
---------------------- ●● A pseudocode written once for a particular problem can be converted
easily to any programming language.
----------------------
●● It is easy to modify.
---------------------- ●● It can be easily written using any editor.
---------------------- Disadvantages
---------------------- ●● It is not visual.

---------------------- ●● Since there is no standard, pseudocode written by two persons will differ.

---------------------- Check your Progress 3


----------------------
Fill in the blanks.
----------------------
1. A ___________Pseudocode is easy to modify.
---------------------- 2. A dry run of the pseudocode or ___________ helps the programmer
to find errors.
----------------------

----------------------
Summary
----------------------
●● Pseudocode is a detailed description of an algorithm or computer program
---------------------- with more focus on readability and understandability and less focus on
---------------------- technical requirements. It is written in a formally styled natural language.
It can be best described as a combination of code and English language.
----------------------

10 Algorithms and Programming Concepts


●● Sequence construct is a list of statements that are executed sequentially Notes
moving from one step to another. Each action is performed one after the
other, from top to bottom and left to right as written. The statements are ----------------------
executed without any condition checking.
----------------------
●● Most often it is required in a code to compare one value with another
for making selection between different choices. Thus, sequence allows ----------------------
selecting a particular option from a given set of choices.
----------------------
●● Repetition statements are also known as looping or iteration statements.
It is required that a certain piece of code is repeated in a pseudocode. ----------------------
Since writing the same code again and again brings a lot of confusion and
makes a lengthy code, these structures are used. ----------------------

----------------------
Keywords
----------------------
●● Sequence: An ordered list.
----------------------
●● Selection: To select from the available list.
●● Iteration: Repetition statement. ----------------------

----------------------
Self-Assessment Questions
----------------------
1. Explain pseudocodes.
----------------------
2. Explain the method of writing pseudocodes for iteration statements.
----------------------
3. Write a pseudocode to accept three numbers and find their average.
----------------------
Answers to Check your Progress
----------------------
Check your Progress 1
----------------------
Multiple Choice Single Response.
----------------------
1. After a programmer plans the logic of a program, he will next:
iv. Code the program ----------------------

2. Which of the following is an assignment operator? ----------------------


i. = = ----------------------
State True or False.
----------------------
1. False
----------------------
2. True
3. False ----------------------

4. True ----------------------
5. True ----------------------

----------------------

Pseudocodes 11
Notes Check your Progress 2
Fill in the blank.
----------------------
1. Iteration is another name for a loop structure.
----------------------
State True or False.
---------------------- 1. True
---------------------- 2. True

---------------------- 3. False
Multiple Choice Single Response.
----------------------
1. Before the user’s selection enters the selection structure for the loop, it
---------------------- should be :
---------------------- i. Validated

---------------------- Check your Progress 3


Fill in the blanks.
----------------------
1. A pseudocode is easy to modify.
----------------------
2. A dry run of the pseudocode or debugging helps the programmer to find
---------------------- errors.

----------------------
Suggested Reading
----------------------
1. Awad, Elias M. Systems Analysis and Design. Illinois: Richard D. Irwin.
----------------------
2. Goel, Anita. Computer Fundamentals. New Delhi: Pearson Education.
---------------------- 3. Huang, Chung-Yang (Ric), Chao-Yue Lai and Kwang-Ting (Tim) Cheng.
“Fundamentals of algorithms”. Electronic Design Automation. Morgan
----------------------
Kaufmann Publishers.
---------------------- 4. ITL Education Solutions Limited. Introduction to Information Technology.
New Delhi: Pearson Education India.
----------------------
5. Sinha, Pradeep and Priti Sinha. Computer Fundamentals. New Delhi:
---------------------- BPB Publications.
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

12 Algorithms and Programming Concepts


Algorithms
UNIT

2
Structure:
2.1 Introduction
2.2 Basics of Algorithm
2.3 Benefits of Algorithm
2.4 Constructs for developing Algorithm
2.4.1 Sequence
2.4.2 Decision
2.4.3 Repetition
2.5 Tools to represent an Algorithm
2.5.1 Flowchart
2.6 Concept of Modularity and Sub-Algorithms
2.7 Concept of Object Oriented Programming
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Algorithms 13
Notes
Objectives
----------------------

---------------------- After going through this unit, you will be able to:

---------------------- l Understand the importance of algorithm


l Discuss the constructs and tools to develop algorithm
----------------------
l Explain modularity and sub-algorithms
----------------------
l Understand the concept of OOP
----------------------

----------------------
2.1 INTRODUCTION
----------------------
We all know that computer is an electronic device and it performs the tasks
---------------------- as per the set of instructions provided to it. The set of instructions is termed as
program. Planning of a program involves defining its logic, that is, defining the
---------------------- correct sequence of instructions needed to solve the problem. The process of
---------------------- defining step- by-step procedure to solve the problem is termed as algorithm.
In this unit, we will try to understand the basics of algorithm along with the
---------------------- different constructs used in designing the algorithm and concept of OOP.
----------------------
2.2 BASICS OF ALGORITHM
----------------------
The term “algorithm” was originally referred to as any calculation or
---------------------- computation performed by applying set of rules applied to numbers. It represents
a solution to a problem. In our day-to-day life we perform many activities and
---------------------- we come across different problems too. To complete these activities or to solve
---------------------- these problems we follow intellectual steps to solve the problem. We follow a
method for carrying out some sequence of events, resulting in accomplishing the
---------------------- task and if we want these tasks to be performed by computer we must somehow
describe it to the computer too.
----------------------
The algorithm is the sequence of steps to take and get the result. It refers
---------------------- to the logic of a program. It is a step-by-step description of how to arrive at a
solution to a given problem. It is defined as a sequence of instructions that when
---------------------- executed in the specified sequence, gives the desired result. Figure 2.1 shows
---------------------- the notion of algorithm.

----------------------

----------------------

----------------------

----------------------

---------------------- Fig. 2.1: Notion of Algorithm

14 Algorithms and Programming Concepts


Let us write the algorithm for a day-to-day life task or activity as “washing Notes
our hands”. The step-by-step process, which is an algorithm, is as follows:
----------------------
Step 1: Start the algorithm.
Step 2: Turn on water. ----------------------
Step 3: Dispense soap.
----------------------
Step 4: Wash hands till clean.
----------------------
Step 5: Rinse soap off.
Step 6: Turn of water.
----------------------

Step 7: Dry hands. ----------------------


Step 8: End the algorithm.
----------------------
Besides algorithm being a finite set of rules that provides a sequence
of activities for solving a specific task, an algorithm must have the following ----------------------
features: ----------------------
1. Finiteness: An algorithm should provide finite number of steps. It must
get terminated or be stopped after performing finite number of steps and ----------------------
each step must be executed finite number of times. ----------------------
2. Definiteness: There should not be any ambiguity in the algorithm. Each
step should be precisely defined. ----------------------

3. Input: Zero or more finite number of inputs must be provided to an ----------------------


algorithm.
----------------------
4. Output: An algorithm must have at least one or more output. This is
very essential else we will not be able to find out whether algorithm has ----------------------
provided solution or not.
----------------------
5. Effectiveness: Each step of an algorithm should be completed with a
finite number of steps. It must be effective to provide finite number of ----------------------
results with finite number of steps.
----------------------
Thus, in order to qualify as an algorithm, a sequence of instructions must
possess the following characteristics: ----------------------

1. Each instruction should be precise and unambiguous. ----------------------


2. Each instruction should be executed in a finite time. ----------------------
3. One or more instructions should not be repeated infinitely. This ensures
----------------------
that the algorithm will ultimately terminate.
4. After executing the instructions (when the algorithm terminates), the ----------------------
desired results are obtained.
----------------------

2.3 BENEFITS OF ALGORITHM ----------------------

Algorithms help to document the “how to” part for accomplishing a ----------------------
particular task. Once the algorithm is written it can be used to solve the similar
----------------------
group of related tasks. Besides this algorithm has following benefits:

Algorithms 15
Notes 1. Better Problem Solving: Algorithm is a systematic process. It involves
identifying inputs, variables and different processes, tasks required for
---------------------- problem solving. This makes problem solving more rational.
---------------------- 2. Improved Efficiency: Algorithms improve the efficiency of solving
the problem. It checks whether all the input variables are considered in
---------------------- problem solving or not. Thus, it makes decision-making more consistent
and efficient.
----------------------
3. Provides Clarity: Algorithm provides clarity. As algorithm is a step-by-
---------------------- step process of problem solving, it provides easy access to identification
of errors and loopholes from the entire process. Thus, it provides clarity
----------------------
of the problem to the problem solver.
---------------------- 4. Provides Reusability: Once the algorithm is written it can be reused for
a similar type of problem solving.
----------------------
Let us see some more examples of algorithm.
----------------------
Example 1: Algorithm to display the addition of three numbers.
----------------------
Step 1: Start the algorithm.
---------------------- Step 2: Read the first number.
---------------------- Step 3: Read the second number.

---------------------- Step 4: Read the third number.


Step 5: Add the three numbers.
----------------------
Step 6: Display the result.
----------------------
Step 7: End the algorithm.
----------------------
Example 2: Algorithm to display the cube of a number.
----------------------
Step 1: Start the algorithm.
---------------------- Step 2: Read the number.
---------------------- Step 3: Calculate cube of the number.

---------------------- Step 4: Display the result.


Step 5: End the algorithm.
----------------------

---------------------- Check your Progress 1


----------------------
Fill in the blanks.
----------------------
1. _________is defined as a sequence of instructions that when executed
---------------------- in the specified sequence gives the desired result.
---------------------- 2. Algorithm must get terminated or stopped after performing finite
number of steps and each step must be executed finite number of
---------------------- times. This feature of an algorithm is termed as_________.

16 Algorithms and Programming Concepts


Match the following. Notes

i. Definiteness a. Solution. ----------------------


ii. Input b. Its each step should be precisely defined. ----------------------
iii. Output c. Finite number of results with finite number of
steps. ----------------------
iv. Effectiveness d. Zero or more finite number of inputs.
----------------------

----------------------
Activity 1 ----------------------

----------------------
Write an algorithm to calculate the area for different geometrical shapes,
such as circle, rectangle, triangle and square. ----------------------

----------------------
2.4 CONSTRUCTS FOR DEVELOPING ALGORITHM
----------------------
Constructs are nothing but different forms with the help of which an
----------------------
algorithm can be built. Constructs make an algorithm not only easy to understand
but also to debug and trace the errors. There are three types of constructs. ----------------------
2.4.1 Sequence
----------------------
In this type of construct the set of instructions are written sequentially.
The instructions are just written one after another. Figure 2.2 shows the ----------------------
diagrammatic representation of the same. ----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Fig. 2.2: Sequence Construct
----------------------
Each action is performed one after the other from top to bottom and left to
right as written. The statements are executed without any condition checking. A ----------------------
sequence construct can be a part of selection and repetition statements as well.
----------------------
Example 3: Algorithm to display the average of three numbers.
----------------------
Step 1: Start
Step 2: Read the first number. ----------------------
Step 3: Read the second number. ----------------------

----------------------

Algorithms 17
Notes Step 4: Read the third number.

---------------------- Step 5: Add the three numbers.


Step 6: Divide the addition of three numbers by 3.
----------------------
Step 7: Display the result.
----------------------
Step 8: End
---------------------- Example 4: Algorithm to read two sides of rectangle and calculate area.
---------------------- Step 1: Start
---------------------- Step 2: Input the width (w) and height (h) of a rectangle.

---------------------- Step 3: Calculate the area of rectangle : width(w)*height(h)

Step 4: Print Area of Rectangle A.


----------------------
Step 5: End
----------------------
Example 5: Algorithm to calculate simple interest.
----------------------
Step 1: Start
---------------------- Step 2: Read the Principal amount.
---------------------- Step 3: Read the rate of interest.
---------------------- Step 4: Read the duration/time.
Step 5: Multiply three values and divide the same by 100.
----------------------
Step 6: Display the result.
----------------------
Step 7: End
----------------------
2.4.2 Decision
---------------------- Many a times while solving a problem we need to perform decision-
making. If some condition is satisfied we need to perform a set of operations
----------------------
and if the condition is not satisfied we need to perform another set of operations.
---------------------- In such type of problem solving or decision-making, selection constructs are
used. Figure 2.3 shows the diagrammatic representation of the same.
----------------------

----------------------

----------------------

----------------------

----------------------

---------------------- Fig. 2.3: Selection Construct

---------------------- In case of selection constructs, often it is required to perform comparison


operation between different choices. For making comparisons relational
---------------------- operators are used. Table 2.1 shows the different comparison operators.

18 Algorithms and Programming Concepts


Table 2.1 Comparison Operators Notes
Symbol Function performed ----------------------
< strictly less than
----------------------
<= less than or equal to
----------------------
> strictly greater than
>= greater than or equal to ----------------------
== equal to ----------------------
!= not equal to ----------------------
Comparisons done using relational operators evaluate either true or false. ----------------------
These are also called Boolean expressions since they return in a Boolean value.
A Boolean expression always consist of two alternatives. ----------------------
Assuming there are two variables a and b containing the values 10 and 11 ----------------------
respectively. Some of the comparison statements are given as below:
----------------------
Table 2.2 Example of Comparison Operator
Comparison Expression Result
----------------------

a + 1 == b true ----------------------
a>b false ----------------------
a!=b true
----------------------
The comparison for the given values is done using the If-End If construct.
It allows the branching of code with the evaluation of the Boolean expression. ----------------------
If the Boolean expression returns true, then the set of statements associated with
----------------------
the true branch is executed. Otherwise the set of statement with the false branch
is executed (if given). ----------------------
Example 6: Algorithm to check whether number is even or odd. ----------------------
Step 1: Start the algorithm.
----------------------
Step 2: Read number a.
Step 3: If a%2==0 ----------------------
Step 4: Display ‘a’ is even ----------------------
Step 5: Else display a is odd.
----------------------
Step 6: End the algorithm.
----------------------
Example 7: Algorithm to find greatest of two numbers.
----------------------
Step 1: Start the algorithm.
----------------------
Step 2: Declare two variables a & b.
Step 3: If a>b ----------------------
Step 4: Display a is largest. ----------------------

Algorithms 19
Notes Step 5: Else display b is largest.

---------------------- Step 6: Display the result.


Step 7: End the algorithm.
----------------------

---------------------- Example 8: Algorithm to print odd numbers from 1 to 100.


Step 1: Start the algorithm.
----------------------
Step 2: Initialize variable N to 1.
----------------------
Step 3: Print the value of N.
----------------------
Step 4: Increment N counter by 2.
----------------------
Step 5: If N>99 then go to Step 6 else go to step 2.
---------------------- Step 6: End the algorithm.
---------------------- Comparison expressions or Boolean expressions can be made more
---------------------- complex with the use of logical operators. Logical operators are used to
control program flow. The different logical operators that are used in a Boolean
---------------------- expression are as follows:
---------------------- Table 2.3: Logical Operators

---------------------- Logical Function


Operator
---------------------- AND Returns true when both the conditions(Boolean expressions) are true
----------------------
OR Returns true if either condition (Boolean expression) is true
----------------------
NOT Returns true if the condition(Boolean expression) is false
----------------------

---------------------- Suppose there is a situation where a decision has to be made based on the
given condition, to check whether a given number is even and also a multiple of
---------------------- seven. In such a scenario, the AND operator can be used. It will check both the
---------------------- conditions of even number and multiple of seven.
Example 9: Algorithm to calculate the tax for an employee.
----------------------
Assume that salary is between 50,000 and 1,00,000, the tax percentage is
---------------------- 5% else tax percentage is 10%.
---------------------- Step 1: Start the algorithm.
Step 2: Read salary
----------------------
Step 3: If salary>50,000 and salary <1,00,000
----------------------
Step 4: tax=salary*0.05
---------------------- Step 5: Else tax=salary*0.10
----------------------

20 Algorithms and Programming Concepts


Step 6: Display the tax value. Notes
Step 7: End the algorithm. ----------------------
Example 10: Algorithm to display the message as per the temperature range. ----------------------
Temperature less than 15: Too Too Cold
----------------------
Temperature greater than or equal to 15 and less than 20: Too cold
----------------------
Temperature greater than or equal to 20 and less than 25: Cold
Temperature greater than or equal to 25 and less than 30: Normal ----------------------

Temperature greater than or equal to 30 and less than 35: Too Hot ----------------------
Temperature greater than or equal to 35: Too Too Hot ----------------------
Step 1: Start the algorithm. ----------------------
Step 2: Read temperature.
----------------------
Step 3: If temperature<15
----------------------
Step 4: Display message as “Too Too Cold”.
----------------------
Step 5: Else If temperature>=15 AND temperature<20
Step 6: Display message as “Too Cold”
----------------------
Step7: Else If temperature>=20 AND temperature<25 ----------------------
Step8: Display message as “Cold” ----------------------
Step9: Else If temperature>=25 AND temperature<30
Step10: Display message as “Normal” ----------------------
Step11: Else If temperature>=30 AND temperature<35
----------------------
Step12: Display message as “Too Hot”
Step13: Else display message as “Too Too Hot” ----------------------
Step14: End the algorithm.
----------------------
2.4.3 Repetition
----------------------
Sometimes in problem solving we need to execute the same set of
instructions repeatedly. In such cases repetition or looping constructs are used. ----------------------
Figure 2.4shows the diagrammatic representation of the same.
----------------------
While(<condition>)
----------------------
{
----------------------
Action(s)
----------------------
}
----------------------
Fig. 2.4: Repetition Construct
----------------------

----------------------

Algorithms 21
Notes Example 10: Algorithm to find the correct seat in a movie theatre.
begin // Algorithm for finding the correct seat in a movie theatre
----------------------
repeat
----------------------
shift along the aisle to the next row of seats
---------------------- until the row number matches with the one printed on the ticket
---------------------- repeat

---------------------- shift across this row to the next seat


until the seat number agrees with the one printed on the ticket
----------------------
end.
----------------------
Example 11: Algorithm to print 1 to 100 numbers.
----------------------
Step 1: Start
---------------------- Step 2: Declare variable n.

---------------------- Step 3: Initialize variables n =1


Step 4: Print value of ‘n’
----------------------
Step 5: n=n+1
---------------------- Step 6: If n==101 go to step 8
---------------------- Step 7: Else go to step 4.
Step 8: End
----------------------
Example 12: Algorithm to find the factorial of a number entered by user.
----------------------
Step 1: Start the algorithm.
---------------------- Step 2: Declare variable n, factorial and count.
---------------------- Step 3: Initialize variables factorial =1 and count=1
Step 4: Read value of ‘n’ from user.
----------------------
Step 5: Repeat steps 5 and 6 till count==n
----------------------
Step 6: Calculate factorial= factorial*n
---------------------- Increment count by 1 i.e. count=count+1
Step 7: Display factorial.
----------------------
Step 8: End the algorithm.
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

22 Algorithms and Programming Concepts


Notes
Check your Progress 2
----------------------
Fill in the blanks. ----------------------
1. In ________ construct the set of instructions are written sequentially.
----------------------
2. In case of selection constructs, often it is required to perform ________
operation between different choices. ----------------------
State True or False. ----------------------
1. If a=90 and b=7 then a<b returns false ----------------------
2. If a= true and b=false then (a AND b) returns false
----------------------

----------------------
Activity 2
----------------------

1. Write an algorithm to read a number from user and check whether it is ----------------------
prime number or not.
----------------------
2. Write an algorithm to print Fibonacci numbers from 1 to 100.
----------------------

----------------------
2.5 TOOLS TO REPRESENT AN ALGORITHM
----------------------
There are three main tools used to represent an algorithm. They are the
flowchart, pseudocode and the actual code. In the previous unit, we have learned ----------------------
in detail about pseudocode. Let us understand flowchart here.
----------------------
2.5.1 Flowchart
----------------------
Flowcharts are also called “Process flowcharts”. They were invented
by Frank Gilberth in 1921. They are pictorial representations of an algorithm. ----------------------
A flowchart does not use sentences but symbols. The words are enclosed in
symbols which are linked by flow lines with conceptual meanings indicated by ----------------------
their shapes. Flowcharts are the most common method of describing algorithms.
----------------------
Flowcharts are classified in four main types as document flow chart, system
flowchart, data flow- chart and program flowchart. Program flow chart is the ----------------------
tool to represent algorithm in pictorial format.
----------------------
Let us understand now the advantages of flowcharts:
1. They show logical relationships clearly. ----------------------

2. They are easy to follow. ----------------------


3. They allow tracing of actions which depend on conditions. ----------------------
4. They can be produced in a standard way as to allow several people to
----------------------
work on them.
----------------------

Algorithms 23
Notes 5. They are useful to the system analyst for experimenting with different
approaches to a particular problem.
----------------------
Table 2.4 shows different symbols to draw flowcharts.
---------------------- Table 2.4 Flow Chart Symbols
---------------------- Sr.No Symbol Symbol Meaning
---------------------- 1. This symbol indicates the beginning or
the end of the flowchart. This is filled
---------------------- with the words “start”, “stop” or the
name of the subroutine. This symbol is
----------------------
oval in shape.
---------------------- 2. This symbol is used to represent
processes like assigning a value to a
---------------------- variable or sorting a number. The shape
---------------------- of this symbol is a rectangle. It has one
arrow going inside to denote the input
---------------------- data to the process and another arrow
leaving from this symbol to denote the
---------------------- processed data.
---------------------- 3. Subroutine, also called subprogram, is a
small part of a main program. Generally,
---------------------- a major problem is subdivided into
functionally different modules and
----------------------
for each module a separate program
---------------------- is written. Each program or module is
called subprogram or subroutine and
---------------------- each subroutine symbol is functionally
independent. The subroutine symbol is
----------------------
used to represent these modules. The
---------------------- symbol is rectangle in shape with double
lines on left and right side. The symbol
---------------------- is filled with the name of the subroutine
and the logic for this subroutine is drawn
----------------------
as a separate flowchart diagram.
---------------------- 4. The parallelogram shaped symbol
is used to represent the input/output
---------------------- operations such as read and write.
---------------------- 5.
This represents the direct data.
----------------------

----------------------

----------------------

----------------------

24 Algorithms and Programming Concepts


6. The rhombus shaped box denotes a point Notes
in a figure where different actions are
taken depending on whether a particular ----------------------
condition is satisfied or not. This means ----------------------
that unlike the process symbol, the
decision symbol has two exit symbols. ----------------------
The decision symbol has two exit points
one for the condition being met (true), ----------------------
and the other for the condition not being ----------------------
met.
----------------------
7.
This represents the sequential access
----------------------
storage.
----------------------
8.
This represents the stored data. ----------------------
----------------------
9. The connector symbol, round in shape,
is used to maintain links between ----------------------
flowchart when the flowchart runs more
than one page or when the same diagram ----------------------
is continued at different locations of ----------------------
the same page. If the same flowchart is
continuing on the next page, at the end ----------------------
of the first page this symbol is used with
user-defined markers. In the beginning ----------------------
of the second page, this symbol is used ----------------------
with the same marker as was used in the
first page. ----------------------
While drawing the flowchart the logic should be thoroughly checked ----------------------
to ensure that no action has been missed out. The flowchart should show a
consistent level of detail. It is important to check the flowchart by passing data ----------------------
through it and checking that the result is as expected. This can be done using ----------------------
the dry running techniques.
There are certain principles to be followed while drawing flowcharts: ----------------------

1. Marking start and end points clearly ----------------------


2. Using standard symbols ----------------------
3. Avoiding crossed lines
----------------------
4. Using simple decisions
----------------------
5. Working in a consistent direction
The three constructs which are used for writing an algorithm are ----------------------
represented graphically by using flowchart symbols in Figure 2.5. ----------------------

Algorithms 25
Notes

----------------------

----------------------

----------------------

----------------------

----------------------
Fig. 2.5: Flowchart Representation of Algorithm Constructs
----------------------
Let us understand some examples of flowchart.
----------------------
Example 13: A flowchart to perform addition of three numbers.
----------------------

----------------------
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Example 14: A flowchart to read two sides of rectangle from user and
----------------------
calculate area of rectangle.
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

---------------------- Example 15: A flowchart to read two numbers from user and find maximum
of them.
----------------------

26 Algorithms and Programming Concepts


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Flowchart can be used for designing the basic concept of the program
in pictorial form but cannot be used for programming purposes. Some of the ----------------------
limitations of the flowchart are given below:
----------------------
• Complex: The major disadvantage in using flowcharts is that when
a program is very large, the flowcharts may continue for many pages, ----------------------
making them hard to follow. Flowcharts tend to get large very quickly and
it is difficult to follow the represented process. It is also very laborious ----------------------
to draw a flowchart for a large program. You can very well imagine the ----------------------
nightmare when a flowchart is to be developed for a program consisting
of thousands of statements. ----------------------
• Costly: Drawing flowcharts are viable only if the problem-solving logic ----------------------
is straightforward and not very lengthy. However, if flowcharts are to
be drawn for a huge application, the time and cost factor of program ----------------------
development may get out of proportion, making it a costly affair.
----------------------
• Difficult to modify: Due to its symbolic nature, any changes or
modification to a flowchart usually requires redrawing the entire logic, ----------------------
and redrawing a complex flowchart is not a simple task. It is not easy
----------------------
to draw thousands of flow lines and symbols along with proper spacing,
especially for a large complex program. ----------------------
• No update: Usually programs are updated regularly. However, the
----------------------
corresponding update of flowcharts may not take place, especially in the
case of large programs. As a result, the logic used in the flowchart may ----------------------
not match with the actual program’s logic. This inconsistency in flowchart
update defeats the main purpose of the flowcharts, that is, to give the ----------------------
users the basic idea about the program’s logic.
----------------------
2.6 CONCEPT OF MODULARITY AND SUB-ALGORITHMS ----------------------

Algorithms help us to understand and solve problem in step-by-step ----------------------


method. When the size and complexity of problem increases we need to divide
----------------------
the problem into smaller problems/tasks, which are known as sub-algorithms.
To solve such problems the outline of the sub-algorithms for each sub-problem ----------------------

Algorithms 27
Notes has to be designed along with how sub-algorithms should be integrated with
overall algorithm. For example, if we want to find the median value of finite
---------------------- number of integers, we have to include sorting of numbers as a sub-algorithm.
There can be several sorting algorithms. Any of the sub-algorithms of sorting
---------------------- has to be integrated with the main algorithm for finding mean value of n integers.
---------------------- This concept of sub-algorithms is implemented by modular programming
approach. Modular programming is a technique of software design, in which
----------------------
the functionality of a program is divided into smaller and interchangeable
---------------------- modules. This approach of modular programming is also called “top-down”
approach or “step-wise refinement” approach. The software modularity also
---------------------- indicates the number of application modules capable of solving a particular
business problem. These modules are divided based on functionality.
----------------------

---------------------- 2.7 CONCEPT OF OBJECT ORIENTED PROGRAMMING


---------------------- Object-oriented programming is based on ‘objects’. The objects are
components having attributes and methods. For example, files, forms, buttons,
---------------------- windows etc., are objects because they have properties such as size, color,
---------------------- caption etc. In Object-oriented programming, program elements are considered
as objects.
----------------------
What is an Object?
---------------------- We have seen that an object is the component having attributes. These
attributes are specified in terms of two parameters, state and behaviour. For
----------------------
example, bicycles have state (gear, pedal cadence, wheels, etc.) and behaviour
---------------------- (braking, accelerating, slowing down, changing gears, etc.). On similar grounds,
a software object maintains its state in variables and implements its behaviour
---------------------- with methods.
---------------------- Alternatively, an object has attributes (properties) and methods (functions).

---------------------- The modern languages such as C++, VC++, VB, Java, .NET, Python &
JavaScript are based on the concept of OOPS. (Object Oriented Programming
---------------------- Systems).

---------------------- A number of programming languages have been developed from the


concept of classes and interfaces. A class defines a set of data structures called
---------------------- ‘variables’, and the operations, referred to as ‘methods’, that are permitted
on the variables. An interface defines a collection of methods that are to be
---------------------- implemented by a class. The classes and interfaces contained in compilation
---------------------- units are organized into ‘packages’. Packages are used to group related classes
and interfaces and to avoid naming conflicts.
---------------------- Java, C++, Smalltalk, and some other object-oriented languages follow a
---------------------- class-based approach. This approach allows us to declare classes that serve as a
template from which objects are created.
----------------------
A class defines the type of data that is contained in an object and the
---------------------- methods that are used to access this data. A class also defines methods to be

28 Algorithms and Programming Concepts


used to create objects that are instances of the class. An instance can also be Notes
understood as an object in its memory space.
----------------------
Analogy with ‘blank and filled-in application forms’
The concept of class and objects can be understood better with the ----------------------
analogy of a prescribed application form. Suppose we have a blank application
----------------------
form. This application format comprises of usual fields such as name, address,
qualifications, date of birth, etc. This blank application form can be treated as ----------------------
a class. When we circulate copies of this form to a number of students in the
class, each student fills in the form. Now, although the fields of each form are ----------------------
the same, the information against these fields is different. For example, the
----------------------
name field will have different records such as Rajesh, Amit, Prasad, Pooja, etc.
These filled-in forms can be called Objects. These objects are instance of class ----------------------
‘application form’.
----------------------
Access modifiers
A Class is a set of data and functions. On the other hand, the structure is ----------------------
also a set of data and functions. However, the main difference between class ----------------------
and structure is that the class supports OOPS features such as encapsulation,
inheritance and polymorphism. The structure does not support OOPS features. ----------------------
The class is divided into two subsets - public and private. ‘Public’ class ----------------------
has global access i.e. access from outside a class is possible. ‘Private’ class does
not have access from outside a class. By default, functions are public and data ----------------------
variables are private. The class layout is shown below figure.
----------------------

Class
----------------------
No access X private : (data) ----------------------

 ----------------------
public : (functions)
access allowed
----------------------

----------------------

----------------------
Fig 2.5: Class layour
Encapsulation: ----------------------

Encapsulation is defined as packaging an object’s variables within the ----------------------


protective custody of its methods. Typically, encapsulation is used to hide
unimportant implementation details from other objects. When you want to ----------------------
change gears on your bicycle, you don’t need to know how the gear mechanism ----------------------
works, you just need to know which lever to move. Similarly in software
programs, you don’t need to know how a class is implemented, you just need to ----------------------
know which methods to invoke.
----------------------
The concept of encapsulation was not available in conventional structured
programming. This concept is introduced in object-oriented programming. ----------------------

Algorithms 29
Notes In java program, data fields and methods are the two main elements. The
encapsulation enables user to hide, inside the object, both the data fields and the
---------------------- methods that act on that data. Thus the user can control access to the data. In
object-oriented programming, an object’s data is always private to the object.
---------------------- Other parts of a program should never have direct access to that data. Thus
---------------------- encapsulation is nothing but data-hiding.
We can always hide data inside functions, just by making that data local to
----------------------
the function. If the data is to be made available to other functions, make the
---------------------- data global to the program, which gives any function access to it. This concept
is available in conventional structured programming and will not serve the
---------------------- purpose of data hiding. The other way is to make our data global to the functions
that need it but still prevent other functions from gaining access. This role is
----------------------
performed by Encapsulation. In an object, the encapsulated data members are
---------------------- global to the object’s methods, yet they are local to the object. However they
are not global variables.
----------------------
One of the characteristics of object-oriented programming that is often touted
---------------------- in discussions of the subject is encapsulation. The term carries the connotation
of an object being enclosed in some sort of container - and that is exactly what
---------------------- it means. Encapsulation is the combining of data and the code that manipulates
that data into a single component - that is, an object. Encapsulation also refers
----------------------
to the control of access to the details of an object’s implementation. Object
---------------------- access is limited to a well-defined, controlled interface. This allows objects to
be self-contained and protects them from accidental misuse, both of which are
---------------------- important to reliable software design.
---------------------- The advantages of encapsulation are:

---------------------- 1) Data hiding: The object has a public interface. That is, the other objects
can communicate with it. However, object can maintain private data and
---------------------- methods that can be changed at any time without affecting the other objects
that depend on it. For example, principle of working and construction of
---------------------- bike is not required to be understood for using the bike.
---------------------- 2) Modularity: The Java language is a modular language. The source code
for an object can be written and maintained independently of the source
---------------------- code for other objects. We can easily pass the objects within a program.
---------------------- For example, we can give our bicycle to others and it will still work.
Inheritance:
----------------------
Inheritance enables you to create a class that is similar to a previously
---------------------- defined class, but one that still has some of its own properties. Suppose that
you have a class for a regular 2 wheeler bike. Let us suppose that we have to
----------------------
create a bike that has an additional high-speed gear. In conventional structured
---------------------- programming, we are required to modify the program extensively. This might
introduce bugs into the program.
----------------------
In order to avoid these problems, we can create a new class by inheritance.
---------------------- This new class inherits all the data and methods from the base class of bike. The

30 Algorithms and Programming Concepts


keywords like public, private, and protected can be used to control the level of Notes
inheritance.
----------------------
The concept of inheritance is derived from the fact that the children inherit
many of their attributes from their parents. In addition to these characters, they ----------------------
also have their own characteristics. In object-oriented programming, a base
class is analogues to a parent and a derived class is analogous to children. ----------------------
The advantages of Inheritance are as follows: ----------------------
1) We can implement super-classes called abstract classes.
----------------------
2) By using inheritance, we can reuse the code in the superclass a number of
times. ----------------------

Polymorphism: ----------------------
Polymorphism is the ability to assume different forms. In programming, ----------------------
it is treated as the ability of objects to have many methods of the same name,
but with different forms. ----------------------
By using polymorphism, we can create new objects that perform the same ----------------------
functions as the base object but which perform one or more of these functions in
a different way. For example, consider area object that finds area of circle. By ----------------------
using polymorphism, you can create area object that computes area of rectangle.
----------------------
We can do this by creating a new method that calculates area of rectangle. Both
the old circle-area and the new rectangle-area methods have the same name say, ----------------------
method area.
----------------------
An example of Encapsulation, Inheritance, and Polymorphism is given below:
We have seen above that the bike is as an object which possesses various ----------------------
attributes such as direction, position and speed. The object bike has various ----------------------
means such as handle, kick and brakes. These means act on the attributes. In
order to construct a class for a bike object, the attributes (direction, position and ----------------------
speed) are considered as the class’s data fields and the means (handle, kick and
brakes) represent methods. ----------------------
The first step in creating an object is to define its class. ----------------------
class bike ----------------------
{
----------------------
data position;
----------------------
data direction;
data speed; ----------------------

method Handle(); ----------------------


method Kick(); ----------------------
method Brake();
----------------------
}
----------------------

Algorithms 31
Notes The above example shows how a class is created. It also shows how
encapsulation works. In the above example, bike is base class. It has three data
---------------------- fields viz. direction, position and speed. These three data fields can be operated
by the three methods Handle (), Kick (), and Brake (). The Handle () method is
---------------------- used to change direction of bike. Similarly the method Kick () is sued to start
---------------------- the bike and the method Brake () stops the bike. Thus position, direction and
speed can be controlled by these three methods. The data fields and methods
---------------------- are encapsulated inside the class. These data fields are private to the class.
Therefore, these data fields cannot be directly accessed from outside of the
---------------------- class. Only the three methods of class can access the data fields.
---------------------- Now, let us create a new bike that has a special gear. To do this, we can
use OOP inheritance to derive a new class from the bike base class.
----------------------
Class newbike inherits from bike
----------------------
{
---------------------- method newstyle();
---------------------- }
---------------------- The class newbike implicitly inherits all the data fields and methods from
the base class of bike. This performs a method called newstyle (). In addition, it
---------------------- also has the direction, position, and speed data fields and three methods Handle
(), Kick (), and Brake ().This is an example of inheritance.
----------------------
Now let us suppose that we want a new kind of bike that has all the
---------------------- characteristics of a newbike, except that its additional gear is twice as fast as the
gear of newbike.
----------------------
class Fastbike inherits from newbike
----------------------
{
---------------------- method newstyle();
---------------------- }
---------------------- The class Fastbike looks exactly like the original class newbike. However,
rather than just inheriting the newstyle() method, it defines its own version. This
---------------------- new version makes the bike move twice as fast as the newstyle () method of
class newbike.
----------------------
In this way, the Fastbike class implements the same functionality as the
---------------------- newbike () class, but it implements that functionality a little differently. Because
the Fastbike class inherits from newbike, which itself inherits from bike, a
----------------------
Fastbike also inherits all the data fields and methods of the bike class. We can
---------------------- control how inheritance using the public, protected, and private keywords.
For example, book is the object that provides access to information (data).
----------------------
The functions open, close, read etc., are associated with the term book.
---------------------- The process of creation of objects is provided by constructor. Using
---------------------- Objects, the program can be simplified. Moreover, the software developed can
be reused by us.
32 Algorithms and Programming Concepts
Messages: Notes
In a pure object-oriented programming model, such as that used by
----------------------
Smalltalk, objects interact by sending messages to each other. When an object
receives a message, the object invokes a method to process the message. The ----------------------
method may change the state of the object, return information contained in the
object, or cause objects to be created or deleted. ----------------------
The object model used by Java is consistent with the concept of message ----------------------
passing, but does not emphasize it. In the Java model, objects interact by
invoking each other’s methods. Methods provide access to the information ----------------------
contained in an object. The type of access varies depending on the method.
----------------------
Software objects interact and communicate with each other by sending
messages to each other. When object A wants object B to perform one of B’s ----------------------
methods, object A sends a message to object B.
----------------------
The message comprises of the following three components:
----------------------
1) The object to whom the message is addressed.
2) The name of the method to perform. ----------------------

3) Any other parameters needed by the method. ----------------------


These three components are enough information for the receiving object ----------------------
to perform the desired method. No other information or context is required.
----------------------
Dynamic Binding:
Sometimes a communication link is required to be established between ----------------------
objects of many different classes. The method for accessing the object is known as ----------------------
dynamic binding. For example, when a Java applet is executed by the web browser,
the applet requires loading of classes located on other computers connected ----------------------
across the Internet. Dynamic binding allows use of new and modified objects.
The recompilation is not required. The compiler provides an executable code. ----------------------
This dynamically interfaces with unknown objects during program execution. ----------------------

Check your Progress 3 ----------------------

----------------------
Fill in the blanks.
----------------------
1. Flowcharts are also called ________.
2. When the size and complexity of problem increases we need to ----------------------
divide the problem into smaller problems/tasks which is known as ----------------------
_________.
3. Approach of modular programming is also called as approach ----------------------
________or _____ approach. ----------------------
4. In case of modular programming the modules are divided based on
______________. ----------------------

----------------------

Algorithms 33
Notes
Activity 3
----------------------

---------------------- Draw flowcharts for all the algorithms written in Activity 1 and Activity 2.

----------------------

---------------------- Summary
---------------------- • The algorithm is the procedure of steps to take and get the result. It refers
to the logic of a program. It is a step-by-step description of how to arrive
----------------------
at a solution to a given problem.
---------------------- • Algorithm is defined as a sequence of instructions which when executed
in the specified sequence gives the desired results.
----------------------
• Finiteness, definiteness, input, output are important features of an
---------------------- algorithm.
• Algorithms help for better problem solving, improves efficiency, provides
----------------------
clarity and reusability.
---------------------- • Sequence, decision and repetition are the three constructs for developing
an algorithm.
----------------------
• Three main tools are used to represent an algorithm. They are flowchart,
---------------------- pseudocode and the actual code.
---------------------- • The pictorial representations of an algorithm are called flowcharts.
---------------------- • When the size and complexity of problem increases we need to divide the
problem into smaller problems/tasks which is known as sub-algorithms.
----------------------
• The concept of sub-algorithms is implemented by modular programming
---------------------- approach.

----------------------
Keywords
----------------------
• Algorithm: Step-by-step procedure for calculations.
---------------------- • Comparison and logical operators: Used with decision-making
---------------------- statements.
• Flowchart: Graphical representation of logic.
----------------------
• Modularity: Process of breaking down complex program into several
---------------------- modules.
----------------------

----------------------

----------------------

----------------------

34 Algorithms and Programming Concepts


Notes
Self-Assessment Questions
----------------------
1. What is an algorithm? Explain the different features of algorithm.
2. Explain the importance of an algorithm in detail. ----------------------
3. What are the characteristics of an algorithm? ----------------------
4. Explain in detail different constructs for building an algorithm. ----------------------
5. What is flowchart? Explain the different flowchart symbols.
----------------------
6. Write the advantages and disadvantages of flowcharts.
----------------------
7. Why is there a need to write sub-algorithms? Explain with a suitable
example. ----------------------
8. What is modular programming? ----------------------
9. Explain the encapsulation.
----------------------
Answers to Check your Progress ----------------------
Check your Progress 1 ----------------------
Fill in the blanks. ----------------------
1. Algorithm is defined as a sequence of instructions that when executed in
the specified sequence gives the desired result. ----------------------

2. Algorithm must get terminated or stopped after performing finite number ----------------------
of steps and each step must be executed finite number of times. This
----------------------
feature of an algorithm is termed as finiteness.
Match the following. ----------------------
i. – b. ----------------------
ii. – d. ----------------------
iii. – a.
----------------------
iv. – c.
----------------------
Check your Progress 2
Fill in the blanks. ----------------------
1. In sequence construct the set of instructions are written sequentially. ----------------------
2. In case of selection constructs, often it is required to perform comparison ----------------------
operation between different choices.
State True or False. ----------------------

1. True ----------------------
2. True ----------------------

----------------------

Algorithms 35
Notes Check your Progress 3
Fill in the blanks.
----------------------
1. Flowcharts are also called process flow charts.
----------------------
2. When the size and complexity of problem increases we need to divide the
---------------------- problem into smaller problems/tasks which are known as sub-algorithms.

---------------------- 3. Approach of modular programming is also called top-down approach or


step-wise refinement approach.
---------------------- 4. In case of modular programming the modules are divided based on
---------------------- functionality.

----------------------
Suggested Reading
----------------------
1. Awad, Elias M. Systems Analysis and Design. Illinois: Richard D. Irwin.
---------------------- 2. Goel, Anita. Computer Fundamentals. New Delhi: Pearson Education.
---------------------- 3. Huang, Chung-Yang (Ric), Chao-Yue Lai and Kwang-Ting (Tim) Cheng.
“Fundamentals of algorithms”. Electronic Design Automation. Morgan
---------------------- Kaufmann Publishers.
---------------------- 4. ITL Education Solutions Limited. Introduction to Information Technology.
New Delhi: Pearson Education India.
----------------------
5. Sinha, Pradeep and Priti Sinha. Computer Fundamentals. New Delhi:
---------------------- BPB Publications.
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

36 Algorithms and Programming Concepts


Designing and Analysing Algorithms
UNIT

3
Structure:
3.1 Introduction
3.2 Desirable Properties of an Efficient Algorithm
3.3 Computational Complexity of Algorithms
3.4 Asymptotic Notations
3.5 Different Analysis of Algorithms
3.6 Example Analysis
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Designing and Analysing Algorithms 37


Notes
Objectives
----------------------

---------------------- After going through this unit, you will be able to:

---------------------- l Understand the fundamentals of designing and analysing algorithms

---------------------- l Discuss the different ways of analysing the efficiency of an algorithm


l Explain different asymptotic notations
----------------------
l Estimate the run time complexity of different algorithms
----------------------

----------------------

---------------------- 3.1 INTRODUCTION


----------------------
Algorithm is a set of procedures that takes some inputs, does some
---------------------- computations and produces an output. Therefore, it is a set of computational
procedures or steps that transforms the input to output. The output produced is
---------------------- different for different inputs or input instances. An instance of a problem can be
defined as a set of all the inputs, which satisfy the constraints imposed on the
----------------------
problem statement to compute a solution to the problem statement.
---------------------- Let us take an example of sorting, which is a basic problem required
---------------------- in almost all areas. There are many sorting algorithms developed, but which
algorithm is best suited in a given situation depends on the number of items
---------------------- to be sorted, the input instance, which means the extent to which the items are
already sorted and the storage medium on which the input instance is stored.
----------------------
Most of the time, algorithms are evaluated or validated on their correctness
---------------------- and ability to give the correct output. A good algorithm should give a correct
output with every input instance and should stop. But there are other criteria
---------------------- also on which the algorithm should be judged and which have a direct impact on
---------------------- its performance. These criteria are: computing time and storage requirements.
Different comparative analyses are required to judge an algorithm for
---------------------- better efficiency. This is done by applying the algorithm for the problems such
---------------------- as selection, sorting and merging. For such problems, most often, the number
of comparisons involved is of more importance rather than the length of the
---------------------- operands. If we assume unit cost for one comparison, then it is not considered
unreasonable, since the size of the input instance does not change during the
---------------------- course of the algorithm. Also, not only are the arithmetic operations, the code
---------------------- of the algorithm, but also the cost of the memory access that is proportional to
the length of the address and the operand.
----------------------
Analysis of algorithms counts the number of operations and not the exact
---------------------- timing of the operations. The number of arithmetic operations is counted at the
bit level. Therefore, the cost of an addition or multiplication is proportional
----------------------

38 Algorithms and Programming Concepts


to the length of the input. Also, the time taken by an algorithm varies on Notes
different machines. There are different variations of the computer with different
architectures and internal organisations. There could be many reasons for the ----------------------
different architectures and organisation, some of it is due to the word length and
number of registers from the machine. ----------------------

----------------------
Check your Progress 1
----------------------
Multiple Choice Single Response. ----------------------
1. The time taken by an algorithm depends upon: ----------------------
i. Architecture of a machine
----------------------
ii. Size of the input
----------------------
iii. None
iv. Both ----------------------
State True or False. ----------------------
1. For judging the efficiency of an algorithm, the number of comparisons ----------------------
is of more importance at times.
2. The efficiency of an algorithm does not depend upon the storage ----------------------
medium of the input instance. ----------------------
3. The efficiency of an algorithm differs with different input instances.
----------------------

----------------------
Activity 1
----------------------
Assume an input instance of ten integers and compare the Bubble Sort
----------------------
and Selection Sort algorithms based on the number of comparisons involved
and the number of swappings required to sort the integers in ascending order. ----------------------

----------------------
3.2 DESIRABLE PROPERTIES OF AN EFFICIENT ----------------------
ALGORITHM
----------------------
Different people can write an algorithm in different ways. There can be
----------------------
different algorithms for the same purpose or objective. Most often, an algorithm
that gives a correct output is considered or selected. The efficiency of the ----------------------
algorithm or the availability of a better algorithm is not taken into consideration.
These factors are only considered in the industry, when the efficiency of the ----------------------
algorithm poses serious performance issues in terms of the time taken and the
----------------------
storage occupied.
For a similar problem, a solution can be written in many ways and it ----------------------
is necessary to identify the correct solution based on certain factors that can ----------------------
make a serious impact on the performance. Along with that, there should be

Designing and Analysing Algorithms 39


Notes a proper specification of problem, which consists of two parts: first, the set of
input instances that are given to the problem and second, the characteristics of
---------------------- the desired output.
---------------------- The Properties of Algorithms: A few properties can be generalised on
the basis of their applicability. An algorithm will benefit from the following
---------------------- properties:
---------------------- a) Input – An algorithm may require an input. The values which are accepted
by the algorithm are termed as ‘input’ or ‘arguments’ or ‘parameters’.
----------------------
b) Output – An algorithm produces a result or output after processing. This
---------------------- is the solution to the definition of the problem.

---------------------- c) Definiteness – Every algorithm has steps. These steps need to be well
defined and logical.
---------------------- d) Accuracy or Correctness - Ideally an algorithm should perform the task
---------------------- that it is designed to perform, for all permutations and combinations of
input. This behavior is consistent for permutations and combinations.
---------------------- e) Finiteness - An algorithm should produce an output after performing a
---------------------- finite number of computational steps.
f) Effectiveness - Every step of the algorithm must be performed precisely,
----------------------
in finite time.
---------------------- g) Generality -- An algorithm written for a specific algorithm should be
applicable to all algorithms of the same general form, with only slight
----------------------
modifications permitted.
---------------------- An algorithm is written in a language independent way. So to analyse the
---------------------- efficiency of the algorithm, we need ways or techniques to do it in a language
independent way. Also identification of the efficiency of an algorithm is done
---------------------- on the basis of the amount of resources required by an algorithm.

---------------------- The objective of analysing an algorithm is to minimise the usage of the


resources. Most often the resources to be considered when analysing an algorithm
---------------------- are the space occupied and the time required executing it. Other factors that
may be considered are: readability, usability, effort of the programmer, number
---------------------- of lines of code etc. Different algorithms have different design techniques as
---------------------- well.
The selection of an algorithm depends on what is the objective or criteria
---------------------- to be achieved:
---------------------- 1. Requirement for high speed
---------------------- 2. Usage of Minimum memory
The analysis of an algorithm is relative to the size of the input. Two different
----------------------
situations are considered:
---------------------- 1. With the same input size but different input instances.
---------------------- 2. Different input size and different instances.

40 Algorithms and Programming Concepts


The basic advantage of analysing an algorithm is that it allows us to make Notes
quantitative judgments about the value of one algorithm over another.
----------------------
Thus the main goal of the analysis of algorithms is to compare algorithms
not only in terms of running time but also in terms of other factors like memory ----------------------
requirements, effort of the programmer etc.
----------------------
Check your Progress 2 ----------------------

State True or False. ----------------------

1. It is possible that the time taken by an algorithm to sort a set of 10 ----------------------


numbers is more than the time taken to sort a set of 30 numbers using
Bubble Sort. ----------------------

Multiple Choice Single Response. ----------------------


1. Major factors that govern the choice of an algorithm over another is ----------------------
the
----------------------
i. Time taken by the algorithm
ii. Space occupied ----------------------
iii. Both ----------------------
iv. None ----------------------
Fill in the blanks.
----------------------
1. A language independent way of analysing the algorithm is required
since algorithms are written in _______ language. ----------------------
2. Analysing an algorithm gives ___________________ judgments of ----------------------
the efficiency of one algorithm over the other.
----------------------

----------------------
3.3 COMPUTATIONAL COMPLEXITY OF ALGORITHMS
----------------------
To increase the efficiency of the algorithms so they can perform with
maximum optimisation, it is essential to calculate the complexity of the ----------------------
algorithms. Even if calculating it exactly may be impossible, approximate
calculation is necessary. Complexity can be evaluated using a few factors. They ----------------------
are:
----------------------
a) Efforts in terms of Efforts E(n) : It focuses on the number of operations
required to be executed to generate a specific required output. ----------------------

b) Memory M(n): It calculates the memory required for an algorithm to be ----------------------


saved in the memory and memory required for execution and processing
of the algorithm and to store the output. ----------------------

c) Time T(n):It measures the time required for complete execution of the ----------------------
algorithm.
----------------------

Designing and Analysing Algorithms 41


Notes d) Cost C(c): It is also termed as “the space-time bandwidth product”. It
calculates the costs of collective computational resources essential for the
---------------------- complete execution of the algorithm.
---------------------- Analysis of an algorithm can be broken down into simple steps. We can
summarise the steps for our convenience as follows:
----------------------
Step 1: Break down the algorithm in sub-modules.
---------------------- Step 2: For each sub-module, complexity of the sub-modules is calculated.
---------------------- This is done using methods which are discussed later.(like Big-Oh
notation)
----------------------
Step 3: The complexity calculated for each sub-module is then used
---------------------- for calculating the complexity for the entire algorithm. i.e. the estimation of
the entire algorithm can be done using the complexity of the sub-modules.
---------------------- An example is discussed here to illustrate the following:
---------------------- Objective: Consider an algorithm for finding the maximum element in
a given sequence of numbers. Hypothesis: consider a set {a(n)}.of all integer
---------------------- elements i.e.,
---------------------- Let {an} = (a1,a2,...,an). Let max be a integer variable.
---------------------- { max = a1 # First I/O operation (Declare and initialise a variable max
with first value in the set. )
----------------------
for i = 2 to n do: # Loop iteration occurs n-1 times (we need to execute the
---------------------- loop till the last element of the set, by comparing each
element in the set.
----------------------
{ if ai max then # One comparison per iteration
----------------------
max := ai } # possibility of one I/O operation (every time the element in
---------------------- the set is greater than the max variable, we need to substitute the value of max
with the value of element in the set. Once the last element is reached, max will
---------------------- hold the maximum or largest value in the set.)
---------------------- Analysis:

---------------------- (1) In the preceding algorithm, we note that there are n-1 comparisons within
the loop.
---------------------- (2) In a randomly ordered sequence, half the values will be less than the mean
---------------------- and a1 would be assumed to have the mean value for purposes of analysis.
Hence, there will be an average of n/2 I/O operations to replace the value
---------------------- max with ai. Thus, there are n/2 + 1 I/O operations.
---------------------- (3) This means that the preceding algorithm is O(n) in comparisons and I/O
operations. More precisely, we assert that, in the average case:
----------------------
W(n) = n-1 comparisons + (n/2 + 1) I/O operations.
---------------------- Let us now understand the concepts and theory in computational
complexity of algorithms. There are different techniques for calculating the
----------------------
same.
42 Algorithms and Programming Concepts
1. RAM Model of Computation Notes
2. Asymptotic analysis of worst case complexity
----------------------
We need ways to identify a correct algorithm from an incorrect one, which
is done with verification. This mathematical verification consists of many parts. ----------------------
Some of them are:
----------------------
1) There should be a clear precise statement of what a programmer is trying
to prove. ----------------------
2) There should be a set of assumptions of things that should be considered ----------------------
to be taken to be true and it should be used as a part of the proof.
----------------------
3) There should be a chain of reasoning that takes from these assumptions to
the statement you are trying to prove. ----------------------
The best case, the worst case and the average case is calculated. ----------------------
Performance evaluation is divided into two major phases:
----------------------
1. a priori estimates
----------------------
2. a posteriori testing
A priori estimates are also called as performance analysis as it is done ----------------------
before implementing an algorithm. While a posteriori testing is also called as ----------------------
performance measurement as it happens after implementing an algorithm.
The amount of memory needed by an algorithm for completion is known ----------------------
as Space Complexity. The space needed by an algorithm has two major parts: ----------------------
1. Fixed part: The fixed part of the algorithm is independent of the number
----------------------
and size of the input and output. This is the space occupied by an algorithm
because of its instructions (i.e. the space of the code), space occupied ----------------------
because of simple variables, constants, derived variables etc.
----------------------
2. Variable Part: The variable part of the algorithm is occupied by the
component variables. The size of the variable part depends upon problem ----------------------
instance, reference variables and the recursion stack space.
----------------------
The amount of computer time needed for completion of an algorithm is
termed as “Time Complexity”. ----------------------

3.4 ASYMPTOTIC NOTATIONS ----------------------

----------------------
Asymptotic is a method to describe a function or behavior in terms of its
limits or without bounds. In graphical format we can say that asymptotic is a ----------------------
straight line which continuously approaches the curve but it meets the curve at
any finite distance. ----------------------
For example, following figure shows that the function x is asymptotic to ----------------------
x+1.
----------------------

----------------------

Designing and Analysing Algorithms 43


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Asymptotic complexity is a method of expressing the main component of
---------------------- the cost of an algorithm in terms of some idealised units of some computational
work.
----------------------
Assume that f1(x) and g1(x) are functions of set of real numbers to the set
---------------------- of real numbers.
We say that f1 and g1 are asymptotic and write
----------------------
f1 (x) ≈ g1(x)
----------------------
if f1(x) / g1(x) = c (constant)
---------------------- There are mainly three asymptotic notations:
---------------------- 1) Big Oh Notation
---------------------- The “O” which is called as Big-Oh is a system of expressing the upper
bound of an algorithm at run time. It denotes the largest time an algorithm
---------------------- might take to execute.
---------------------- If there exists an integer and a constant c1>0 for all the integers for all
non-negative function f1(n) and g1(n) and if f1(n)<=c1g1(n) the function f1(n)
---------------------- is a Big O of g1(n). This is also denoted as “f1 (n)=O(g1(n))”.Thus g1(n) serves
as a upper bound to the curve of the function f1(n)
----------------------
Best Case Big Oh notation: This illustrates or shows an upper bound for
---------------------- best case input.
---------------------- Worst Case big Oh notation: describes an upper bound for all input
---------------------- Average Case: is similar to worst case.
2) Small Oh(Theta) Notation
----------------------
Best Case Theta notation: not used
----------------------
Worst Case: describes asymptotic bounds for worst case input
---------------------- Average Case: is similar to worst case.
---------------------- 3) Omega Notation
---------------------- The Omega notation is the asymptotically tight lower bound for a given
curve or function.
----------------------

44 Algorithms and Programming Concepts


Best Case Omega notation: describes a lower bound for all input and Notes
describes the best possible output.
----------------------
Worst Case: describes a lower bound for worst case input, which will be
greater than the best case. ----------------------
Average Case: is similar to best case.
----------------------
Notation Name
----------------------
O(1) Constant
O(log log n) Double logarithmic ----------------------
O(log n) Logarithmic
----------------------
O(n c), 0<c<1 Fractional power
O(n) Linear ----------------------
O( n log * n) n log-star n
----------------------
O(n log n) Log linear
O(n 2) Quadratic ----------------------
O(n c), c>1 Polynomial or algebraic
----------------------
O(c n), c>1 Exponential
O(n!) Factorial ----------------------

----------------------
3.5 DIFFERENT ANALYSIS OF ALGORITHMS
----------------------
The “running time” of the algorithm is represented as a function of the
input size n, f1(n). For comparison of two algorithms with running time f1(n) ----------------------
and g1(n), a rough estimate can be made that is independent of the machine
----------------------
time, programming style etc.
Let us take an example for computing the running time of an algorithm. ----------------------
The goal or the objective is to search a list of elements. Given: a list of n elements
----------------------
which may be either ordered or unordered; the ordering may be numerical or
alphabetical; and an element x that needs to be searched in the list. The goal is ----------------------
to determine whether x appears in the list and if it does, then what is the index
position of the element x. ----------------------
The algorithm is as given below: ----------------------
procedure linear search1 (x1 : integer, a1 , a2 , …, an: distinct integers) ----------------------
i := 1
----------------------
while (i <= n )
i := i + 1 ----------------------
if x1 = ai then ----------------------
location1 := i
return location1 ----------------------
end if ----------------------
end while
----------------------

Designing and Analysing Algorithms 45


Notes For computing the running time of the algorithm, a cost is associated with
every statement and the total cost is found by calculating the total number of
---------------------- times each statement is executed. The cost is assumed to be as a constant which
is independent of the architecture and internal organisation of the computer.
----------------------
Statement Cost No of times Final Cost
---------------------- i := 1 cst1 1 cst1
---------------------- while (i <= n ) cst2 n+1 (n+1)*cst2
i := i + 1 cst3 N n*cst3
----------------------
if x1 = ai then cst4 N n*cst4
---------------------- Location1 := i cst5 1 cst5
return location1 cst6 1 cst6
----------------------
Total Cost = cst1+ (n+1)*cst2+n*cst3+n*cst4+cst5+cst6
----------------------
= cst1+n*cst2 + cst2 +n*cst3 + n*cst4 + cst5 + cst6
---------------------- = (cst2+cst3+cst4) *n +(cst1+cst2+cst5+cst6)
---------------------- (cst2+cst3+cst4) can be written as C1 and (cst1+cst2+cst5+cst6) can
be written as C2. This implies that the total cost of the algorithm turns out to
---------------------- be a linear function, where C1 and C2 are constants which are dependent on
---------------------- architecture and internal organisation of the computer.
This linear function is of order n or O(n) and is at most roughly proportional
---------------------- to n. Hence we say that the time complexity of the linear search algorithm is
---------------------- O(n).
Similarly, another function g(n) =n2 +1 is a quadratic function and is of
----------------------
order n2 , or O(n2). It is, at most, roughly proportional to n2.
---------------------- an O(n2) algorithm will be slower than O(n) algorithm.
---------------------- Warning: an O(n2 ) function will grow faster than an O(n) function.

---------------------- We say that n4 + 100n2 + 10n + 50 is of the order of n4 or O(n4)


We say that 10n3 + 2n2 is O(n3)
----------------------
We say that n3 - n2 is O(n3)
----------------------
We say that 10 is O(1),
---------------------- We say that 1273 is O(1)
---------------------- For the given algorithm, the rate of growth is equivalent to the asymptotic
analysis, which means that when rate of growth is used as a measure to compare
---------------------- different functions, it implies comparing them asymptotically.
---------------------- If f1(x) is growing faster than g1(x), then f1(x) eventually becomes larger
than g1(x), for large values of x.
----------------------
For example : If we are designing a web site to process user data
---------------------- such as financial records and assume that its program X takes fX(n)=30n+8
microseconds to process any n records, while program Y takes fY(n)=n2+1
---------------------- microseconds to process the n records. Then how do we select a program which

46 Algorithms and Programming Concepts


satisfies millions of users? Notes
On a graph, as you go to the right, a faster growing function eventually
----------------------
becomes larger.
----------------------

----------------------

----------------------

----------------------

How do we examine algorithms? ----------------------


We need to define a number of objective actions. ----------------------
(1) To compare execution time: ----------------------
Not good: times are specific to a particular computer.
----------------------
(2) To count the number of statements to be executed:
----------------------
Not good: number of statements varies with the programming language
as well as the style of the individual programmer. ----------------------
For Example: As shown in the diagram:
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
3.6 EXAMPLE ANALYSIS
----------------------
When algorithms are considered or reflected the following terms are used
very often: ----------------------
Tractable - An algorithm belongs to a class P, such that the algorithm is ----------------------
solvable in polynomial time. Hence this refers the use of P for polynomial. This
means that complexity of the algorithm is O(nd), where d may be large (which ----------------------

Designing and Analysing Algorithms 47


Notes typically implies slow execution).
Intractable – Here the algorithms requires greater amount of resources
----------------------
than polynomial work, time, space or cost. Approximate solutions to such
---------------------- algorithms are often more efficient than exact solutions and are preferred.
Solvable -- An algorithm exists that generates a solution to the problem
----------------------
addressed by the algorithm, but the algorithm may not be necessarily tractable.
---------------------- Unsolvable -- No algorithm exists for solving the given problem. Example:
Turing showed that it is impossible to decide whether or not a program will
----------------------
terminate on a given input.
---------------------- NP is the set of all decision problems for which the instances where the
---------------------- answer is «yes» have efficiently verifiable proofs of the fact that the answer
is indeed «yes». In an equivalent formal definition, NP is the set of decision
---------------------- problems where the “yes”-instances can be accepted in polynomial time by
a non-deterministic Turing machine.
----------------------
Class NP -- If a problem is in Class NP i.e non-polynomial, then no
---------------------- algorithm with polynomial worst-case complexity can solve this problem.
---------------------- Class NP-Complete -- If any problem is in Class NP-Complete, then any
polynomial-time algorithm that could be used to solve this problem could solve
---------------------- all NP-complete problems. Also, it is generally accepted, but not proven, that
no NP-complete problem can be solved in polynomial time.
----------------------
The term analysis of algorithms is used to describe approaches to the
---------------------- study of the performance of algorithms. In this course we will perform the
following types of analysis:
----------------------
a. The worst-case runtime complexity of the algorithm is the function
---------------------- defined by the maximum number of steps taken on any instance of size a.
---------------------- b. The best-case runtime complexity of the algorithm is the function defined
by the minimum number of steps taken on any instance of size a.
----------------------
c. The average case runtime complexity of the algorithm is the function
---------------------- defined by an average number of steps taken on any instance of size a.

---------------------- d. The amortised runtime complexity of the algorithm is the function defined
by a sequence of operations applied to the input of size a and averaged
---------------------- over time.
---------------------- Example
Let us consider an algorithm of sequential searching in an array of size n.
----------------------
Its worst-case runtime complexity is O(n)
----------------------
Its best-case runtime complexity is O(1)
---------------------- Its average case runtime complexity is O(n/2)=O(n)
---------------------- Example: Linear Search
----------------------

48 Algorithms and Programming Concepts


Objective: Consider an algorithm for finding or locating an element in a Notes
cluster of elements i.e searching of an element.
----------------------
Hypothesis: consider a set {a(n)}.of all un-sorted integer elements i.e.,
Execution Steps: Consider a set {a(n)}.of all integer elements. Element ‘e’ to ----------------------
be searched.
----------------------
Step 1: Read the Input a[1] to a[n] //iterate the loop from a(i) to a(n).
Step 2: Check if a[i] = e ----------------------
Step 3: Iterate from i=1 to i=n; ----------------------
Step 4: when a[i]=e , element found. ----------------------
End;
----------------------
Analysis:
----------------------
The above algorithm consists of one loop, which has n iterations. It takes
a minimum (maximum) of 1 (n) comparisons, for an average work requirement ----------------------
of (n+1)/2 comparisons.
----------------------
Illustration of Linear Search: Given a five-element vector a = (2, 8, 32, 7,
4), let us search for the element a[i] = 7, i = 1…5. The following table suffices ----------------------
to exemplify linear search:
----------------------
STEP i VALUE a[i] CHECK RESULT NEXT i
---- ------- ---- ------- ------- ------- ----------------------
1 1 2 a[1] = 7? 2!= 7 2 ----------------------
2 2 8 a[2] = 7? 8!= 7 3
3 3 32 a[3] = 7? 32!= 7 3 ----------------------
4 4 7 a[2] = 7? 7=7 stop ----------------------
The best case for a linear search algorithm is to have the value e for which one
is searching located at the first position of the ADT. This obviously requires a ----------------------
constant number of comparison operations, i.e., the work is O(1) comparison.
----------------------
In the worst case, e is located at the nth position of the ADT, for which the
associated work is O(n). ----------------------
The Search can also be carried out using Trees. Search trees are usually
----------------------
binary, that is, there are two children per internal node. This allows one to use
a binary relation such as greater-than or less-than to construct a min-tree or ----------------------
max-tree that has, at the root of the tree, the minimum or maximum of the data
stored in the tree. ----------------------
A more generic case is as follows: ----------------------
An n-ary search tree is a rooted tree that has n children per nonterminal ----------------------
node.
In an n-ary search tree of depth L levels, O(L) operations are required to ----------------------
find a prespecified value stored in one of the tree nodes. Given m data items ----------------------
stored in a complete n-ary search tree, the number of levels is computed as
follows: ----------------------

Designing and Analysing Algorithms 49


Notes L = ceil (lognm)
where ceil denotes the ceiling function.
----------------------
Analysis: The disadvantage of some types of search trees, such as binary
---------------------- search trees, is that the sequence stored in the tree must be presorted in order to
have the order property required to implement O(log n) work in the selection
----------------------
process. But, other types of search trees, such as some implementations of
---------------------- binary search trees, AVL trees and red-black trees require only O(log n) work to
insert and delete an element from the tree.
----------------------
Example:
----------------------

----------------------

----------------------

----------------------
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Figure 3.1: Schematic Diagram of Bubble Sort
----------------------
The above is the schematic diagram of bubble sort applied to input vector
---------------------- (2,1,4,2,3) to yield sorted vector (1,2,2,3,4).
---------------------- Objective: Consider an algorithm for sorting elements in an unordered
group of elements using bubble sort.
----------------------
Hypothesis: consider a set {a(n)}.of all un-sorted integer elements
----------------------
Execution Steps: Consider a set {a(n)}.of all integer elements. Local
---------------------- variable min, pos, temp
Step 1: Read the Input a[1] to a[n] //iterate the loop from a(i) to a(n).
----------------------
Step 2: Execute Swap(a[i],a[pos])
---------------------- Swap: from a[1] to a[n]
---------------------- When a[1] ; Set min = Set a[1]; Set pos=a[1]; Set temp[1]=min;
Step 3: Iterate from i=1 to i=n;
----------------------
Step 4: when a[i]=n, temp would be sorted.
---------------------- End;
---------------------- Analysis: The above algorithm consists of one loop, which has n iterations
and takes the efforts of (n-i+1) comparisons per iteration. Hence the complexity
50 Algorithms and Programming Concepts
of the algorithm is calculated to be is O(n<sup2< sup=””>) comparisons. Notes
In the best case scenario, the input is pre-sorted and bubble-sort requires
----------------------
a minimum number of I/O operations.
In the worst case scenario, the input is sorted opposite to the desired order ----------------------
of the sorted output and a maximum number of I/O operations are required.
----------------------

Check your Progress 3 ----------------------

----------------------
State True or False.
1. To increase the efficiency of the algorithms so that they can perform ----------------------
with maximum optimisation, it is essential to calculate the complexity ----------------------
of the algorithms.
----------------------
2. Oh notation is an Asymptomatic Notation.
3. Any algorithm can have two cases of complexity. ----------------------
Fill in the blanks. ----------------------
1. Complexity can be evaluated using following factors: (a) ___________, ----------------------
(b) ____________, (c) ______________ and (d) __________.
2. The Space needed by an algorithm has two major parts: _________ ----------------------
and _____________. ----------------------
3. The amount of computer time needed for completion of an algorithm
----------------------
is termed as _____________.
----------------------
Summary ----------------------

• Algorithm is a set of procedures that takes some inputs, does some ----------------------
computations and produces an output.
----------------------
• The accepted values by the algorithm are termed ‘input’ or ‘arguments’ or
‘parameters’. ----------------------
• Every algorithm has steps. These steps need to be well defined and logical. ----------------------
• Algorithm is written in a language independent way.
----------------------
• Complexity of algorithms is in general represented using Work, Space,
Time and Cost. ----------------------

• Performance evaluation is measured using a priori estimates and a ----------------------


posteriori testing.
----------------------
• Asymptotic is a method to describe a function or behavior in terms of its
limits or without bounds. ----------------------

----------------------

----------------------

Designing and Analysing Algorithms 51


Notes Keywords
----------------------
• Complexity of algorithms: Complexity of algorithms is in general
---------------------- represented using Work, Space, Time, Cost.
• A priori estimates: This is also called as performance analysis as it is
----------------------
done before implementing an algorithm.
---------------------- • Asymptotic Notations: Asymptotic is a method to describe a function or
behavior in terms of its limits or without bounds.
----------------------

---------------------- Self-Assessment Questions


----------------------
1. What is Asymptotic Notations?
---------------------- 2. State the properties of a good algorithm?
---------------------- 3. What are the parameters for measuring the complexity of an algorithm?

---------------------- 4. Analyse algorithms for selection of integer elements both linear and
binary.
----------------------
Answers to Check your Progress
----------------------
Check your Progress 1
----------------------
Multiple Choice Single Response.
----------------------
1. The time taken by an algorithm depends upon:
---------------------- iv. Both
---------------------- State True or False.
---------------------- 1. True
2. True
----------------------
3. False
----------------------
Check your Progress 2
----------------------
State True or False.
---------------------- 1. True
---------------------- Multiple Choice Single Response.

---------------------- 1. Major factors that govern the choice of an algorithm over another is the
iii. Both
----------------------

----------------------

----------------------

----------------------

52 Algorithms and Programming Concepts


Fill in the blanks. Notes
1. A language independent way of analysing the algorithm is required since
----------------------
algorithms are written in true language.
2. Analysing an algorithm gives quantitative judgments of the efficiency of ----------------------
one algorithm over the other.
----------------------
Check your Progress 3
----------------------
State True or False.
1. True ----------------------

2. False ----------------------
3. False ----------------------
Fill in the blanks.
----------------------
1. Complexity can be evaluated using the following factors: (a) Efforts,
----------------------
(b) Memory, (c) Time and (d) Cost.
2. The Space needed by an algorithm has two major parts: Fixed and ----------------------
Variable. ----------------------
3. The amount of computer time needed for completion of an algorithm is
----------------------
termed as Time Complexity.
----------------------
Suggested Reading ----------------------
1. Horowitz, Ellis, Sartaj Sahni, Sanguthevar Rajasekaran. Fundamentals of ----------------------
Computer Algorithms. Galgotia Publications.
2. Huang, Chung-Yang (Ric), Chao-Yue Lai. Fundamentals of Algorithms. ----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

Designing and Analysing Algorithms 53


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

54 Algorithms and Programming Concepts


Writing Language-Neutral Algorithms
UNIT

4
Structure:
4.1 Introduction
4.2 Need for Language-Neutral Algorithms
4.3 Components of an Algorithm
4.4 Standard Format for writing an Algorithm
4.4.1 Predefined Keywords for an Algorithm
4.5 Organisation of an Algorithm
4.6 Verifying if Algorithm is Language-Neutral
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Writing Language-Neutral Algorithms 55


Notes
Objectives
----------------------
After going through this unit, you will be able to:
----------------------
l List the components of an algorithm
----------------------
l Identify the keywords used in an algorithm
---------------------- l Write language-neutral algorithm
---------------------- l Organise the logical steps to an algorithm

---------------------- l Identify if the algorithm is language-neutral or not

----------------------
4.1 INTRODUCTION
----------------------
In the previous unit, we studied about algorithm and the basic design
----------------------
structure of an algorithm. It is important to be able to identify the activities
---------------------- needed to be performed by a program precisely and craft them into an algorithm.
After studying the design and complexity of an algorithm let us now
----------------------
proceed to writing algorithm which would enlist the desired set of activities to
---------------------- be performed by the program that needs to be designed based on it.

---------------------- Some of the questions we might ask while writing an algorithm are:
• What language must be used for writing an algorithm?
----------------------
• What is the structure of an algorithm?
----------------------
• What kind of terms should be used in an algorithm?
---------------------- • What is the organisation of an algorithm?
---------------------- • What checks are needed at the end of the algorithm?

---------------------- In this unit, we will answer these questions and learn the procedure of
writing a language-neutral algorithm. We will learn about the various ways we
---------------------- can add conditions or change the flow of an algorithm.
----------------------
4.2 NEED FOR LANGUAGE-NEUTRAL ALGORITHMS
----------------------
When we decide to write an algorithm, the primary question that arises
---------------------- is to which language to use to write the algorithm. The answer is ENGLISH!

---------------------- Algorithms should be language-neutral, by that we mean it should be


independent of any programming language-specific terms.
---------------------- We do not use any programming language to write any algorithm.
---------------------- Primarily because:
• Algorithms are developed before the actual programs are written.
----------------------
• Algorithms depict the need to solve a problem, actual procedure to solve
---------------------- the problem if implemented later on.

56 Algorithms and Programming Concepts


• Algorithms are building blocks for efficient and logical problem solving. Notes
• Algorithms help to identify the best way to arrive at solution. Choice of
----------------------
programming language is based on best way to implement the algorithm.
So precisely, algorithm is not about code or programming language. It is ----------------------
the logical sequence of steps to derive the solution in best possible way.
----------------------
It is important to write language-neutral algorithms because:
----------------------
• In that case, the algorithms can be implemented in any language of choice.
• In that case, the algorithms can be implemented in any language based on ----------------------
need. ----------------------
• The algorithms may be written in one language and used as an independent
module in heterogeneous setup. ----------------------

• Choice of language should not decide the logic of solution. ----------------------


• Effectiveness of an algorithm is not based on simplicity but on efficiency. ----------------------

Check your Progress 1 ----------------------

----------------------
State True or False.
----------------------
1. Algorithms must be dependent on programming language.
----------------------
2. Algorithms are steps to solving a program logically.
----------------------

----------------------
4.3 COMPONENTS OF AN ALGORITHM
----------------------
Before we start writing an algorithm we must know the problem to be
solved. ----------------------
Algorithm to solve a problem is an efficient mechanism of arriving at the ----------------------
solution. So to solve a problem we need to design an algorithm for the solution.
Understanding the algorithm used to solve the problem helps us to predict ----------------------
the normal flow, special cases if any, cases in which software won’t work ----------------------
quickly, cases where the results would be undefined or unacceptable.
----------------------
Algorithms can be written in many ways and forms like charts, diagrams,
imperative programs, etc. For software algorithms we prefer to write in program- ----------------------
like structure in plain English.
----------------------
Algorithm has the following components:
• Input ----------------------
• Sequence of steps ----------------------
• Output ----------------------

----------------------

Writing Language-Neutral Algorithms 57


Notes It is depicted using the diagram below.

---------------------- Algorithm

----------------------

----------------------

----------------------

----------------------

---------------------- Fig. 4.1: Components of an Algorithm


---------------------- 1. Input: Input of the algorithm must be clearly defined. It could be values
of variables, environment conditions, situation, restrictions, etc.
----------------------
2. Sequence of Steps: Various steps are involved in solving the problem
---------------------- and converting the input to derive the desired output.

---------------------- Steps may be executed in straight sequential manner or repeated until


certain condition is met. For any given scenario or set of inputs the algorithm
---------------------- will have fixed set of steps that are executed.
---------------------- 3. Output: Output of an algorithm is the derived resultant. Expected type of
output(s) must be clearly defined. Any unexpected result is not output.
----------------------
Thus, an algorithm can be defined to be “a high-level description of a
---------------------- procedure which manipulates well-defined input data to produce other data”.
If the problem is complex enough, it must be broken down into simple
----------------------
problems and each solved individually and collated later to derive the solution
---------------------- to original complex problem.

---------------------- For example, if we need to find the average score of children in a given
age group, then we can find average score for each standard in the age group
---------------------- and find the average of these numbers to get the desired result.

---------------------- Input data needs to undergo all the required changes and operations to
form the desired output. This demands the need to store and manage data values
---------------------- – initial, intermittent, final.

---------------------- Data Structure


Data that we deal with in an algorithm is organised into data structures,
----------------------
e.g., list of values or set of numbers, etc.
---------------------- Data structures help in storing the computation of results from various
operations performed during execution of a program. So we may use first-in-
----------------------
first-out list to store results in certain case or last-in-first-out list in another
---------------------- depending on the need. These lists are examples of data structures.
Finally, computer program is the implementation of the algorithm and the
----------------------
underlying data structures used. Hence, algorithm plays a key role in deciding
---------------------- the structure and requirements of a computer program.

58 Algorithms and Programming Concepts


Notes
Check your Progress 2
----------------------
Fill in the blanks. ----------------------
1. Algorithm involves _______, steps and ________.
----------------------
2. Complex algorithm can be broken down into ____________ ones.
----------------------

----------------------
Activity 1
----------------------
Write the steps required to arrange five numbers in increasing order. ----------------------

----------------------
4.4 STANDARD FORMAT FOR WRITING AN ALGORITHM ----------------------
Now let us start learning how to write algorithms. ----------------------
Example 1: Write an algorithm to find largest of three numbers.
----------------------
Solution:
----------------------
Let us see the input that is given to us. Here we have to find largest of
three numbers. So the input would be three numbers. Since we do not know the ----------------------
values of the numbers, let us say the numbers are A, B, C.
----------------------
Input is: Numbers A, B, C
----------------------
Now let us think of the procedure to find the largest of these three numbers.
We know we can find largest of two numbers by comparing them. So for largest ----------------------
of three numbers we can start with comparing two numbers at a time. (We broke
down complex unknown problem into simple known problem here). ----------------------
Step 1: Compare A & B. ----------------------
What if A is greater than B, is that the largest number in the set? No. We ----------------------
need to compare it with C.
Step 2: If A > B then Compare A & C ----------------------

If A is also greater than C then is A the largest number? Yes. ----------------------


Step 3: If A > C then A is largest number in the set. ----------------------
What if C is greater than A?
----------------------
Step 4: Else If C> A then C is the largest number in the set.
----------------------
Good! What if in very first check at Step 1 if A is not greater than B?
Step 5: Else If B> A then Compare B & C ----------------------

Step 6: If B > C then B is the largest number in the set. ----------------------


Step 7: Else if C> B then C is the largest number in the set. ----------------------

Writing Language-Neutral Algorithms 59


Notes Ok! Are there any other comparisons needed? No. So what is the output?
Write the largest number as Output.
----------------------
The following are some simple rules to write an algorithm:
----------------------
1. Use simple English language.
---------------------- 2. Clearly define input.
---------------------- 3. Use indentation to indicate sub-processes.
---------------------- 4. Clearly mention the output.
4.4.1 Predefined Keywords for an Algorithm
----------------------
In case of the above algorithm we used ‘if’ to indicate a condition to be
---------------------- tested. It is a standard keyword for writing an algorithm.
---------------------- So what is a keyword?

---------------------- Keyword is a commonly known word, which has some predefined meaning
associated with it because of which it cannot be used for naming variables or
---------------------- data structures and must be used in context of the original meaning.

---------------------- Here we are using italics to indicate a keyword. ‘…’ indicates there are
other steps which will be performed here depending on the logic. Let us see an
---------------------- example of a keyword.
---------------------- if.. then ..else..
For example ‘if’ indicates there is a condition, so it is a keyword. Similarly
----------------------
‘then’ and ‘else’ are also keywords used with ‘if’
---------------------- If a>b then …else …
---------------------- Let us check other keywords.

---------------------- for ..do..


We use “for” to indicate the range or set of values we must repeat the same
---------------------- activity for.
---------------------- for a = 1,2,3 do ….
---------------------- for a = 1 to 5 do ….increment a

---------------------- for a between 1 and 5 do ….increment a


while …do..
----------------------
We use while to repeat certain steps.
----------------------
while a<5 do… increment a
---------------------- while b> 7 do … decrement b
---------------------- do.. while..

---------------------- There is another scenario when we need to perform certain activity and
then check if we must stop. For example, reducing the number until number is
---------------------- positive, etc.

60 Algorithms and Programming Concepts


do … while a > 4 Notes
This is same as repeat …until..
----------------------
go to
----------------------
When we need to write specific step, we use go to
go to step 4 ----------------------

case ----------------------
There are other keywords which are not depend on condition but consider ----------------------
set of cases.
----------------------
When we have specific scenarios in which we want to perform certain
activity, we can specify it as case. ----------------------
case a is 1: a is neither prime nor co prime…
----------------------
case a = 1, 2, 3: ….
----------------------
Some more keywords are return, break, and continue. These are used
to navigate from one step to another in an algorithm, not necessarily based on ----------------------
some condition.
----------------------
We shall see the uses of these keywords in the next unit. For now let us
quickly list all the keywords. ----------------------
If, then, else, for, do, while, repeat, until, go to, case, return, break, ----------------------
continue.
----------------------
Check your Progress 3 ----------------------

Fill in the blanks. ----------------------

1. Algorithms should be written in simple _________ language. ----------------------


2. ____________ should be used to indicate sub-processes in the ----------------------
algorithm.
----------------------

----------------------
Activity 2
----------------------
Identify keywords used in writing algorithms from the following set of ----------------------
words:
----------------------
how, if, where, then, case, pull, terminate, add, for, follow, do, jump, while.
----------------------

4.5 ORGANISATION OF AN ALGORITHM ----------------------

When we know how to write an algorithm, it is equally important to ----------------------


understand the way to organise steps in an algorithm to make it efficient and ----------------------
simple to understand.

Writing Language-Neutral Algorithms 61


Notes • Organisation of an algorithm should be from simple to complex, such that
simple statements combine together to solve a complex problem. Simple
---------------------- addition, subtraction operation can be used to perform mathematical
computation and comparison.
----------------------
• We must move from what are known values to find the unknown values.
---------------------- There should be no guesswork involved.
---------------------- • All the cases or scenarios must be covered. If any scenario is left then
program would not perform any activity and may hang in that loop.
----------------------
• The steps required to solve the problem must be arranged sequentially.
---------------------- • Navigation between steps must be clear.
---------------------- • Standard keywords must be used.
---------------------- • At point of time it should be clear which step needs to be performed.
• There should be no ambiguity.
----------------------
Let us check the organisation of the algorithm we wrote for finding largest
---------------------- of three numbers.
---------------------- Step 1: Compare A & B.

---------------------- Step 2: If A > B then Compare A & C


Step 3: If A > C then A is largest number in the set.
----------------------
Step 4: Else If C> A then C is the largest number in the set.
----------------------
Step 5: Else If B> A then Compare B & C
---------------------- Step 6: If B > C then B is the largest number in the set.
---------------------- Step 7: Else if C> B then C is the largest number in the set.

---------------------- Let us check the organisation of this algorithm


• We have first compared two numbers – A & B, and then compared other
---------------------- two numbers – A & C and B & C. This is done one after the other and not
---------------------- parallel.
• We did not write a step to compare A & C and B&C at the same stage.
----------------------
• We went from known values of three numbers, comparing them to find
---------------------- the largest. There are no assumptions.
---------------------- • Standard keywords such as if, then, else are used. No new unknown
keywords are introduced.
----------------------
• All the scenarios are covered.
---------------------- • Navigation in steps is clear.
---------------------- • Statements are not ambiguous.
---------------------- It is important to organise the steps in algorithm to ensure it is clearly
understood.
----------------------

62 Algorithms and Programming Concepts


Notes
Check your Progress 4
----------------------
Multiple Choice Multiple Response. ----------------------
1. Which of these should be checked in the organisation of an algorithm?
----------------------
i. Ambiguity
----------------------
ii. Length
iii. Keywords ----------------------

iv. Data type ----------------------

----------------------
4.6 VERIFYING IF ALGORITHM IS LANGUAGE- ----------------------
NEUTRAL
----------------------
It is very important to write language-neutral algorithms. Otherwise the
algorithm may be programmable in a specific language but not in any other ----------------------
programming language which is best suited for it.
----------------------
To verify if algorithm is language-neutral:
----------------------
1. List all the words used in each step apart from input data, variables and
output statement. ----------------------
2. Remove the words which indicate mathematical or logical computation ----------------------
like increment, compare, etc.
----------------------
3. Does the list consist of standard keywords? If yes, then algorithm is
language-neutral. ----------------------
4. If not replace them with suitable keywords.
----------------------
Let us check if our algorithm is language-neutral.
----------------------
Step 1: Compare A & B.
Step 2: If A > B then Compare A & C ----------------------

Step 3: If A > C then A is largest number in the set. ----------------------


Step 4: Else If C> A then C is the largest number in the set. ----------------------
Step 5: Else If B> A then Compare B & C
----------------------
Step 6: If B > C then B is the largest number in the set.
----------------------
Step 7: Else if C> B then C is the largest number in the set.
1. List all the words used in each step apart from input data, variables and ----------------------
output statement. ----------------------
List is - compare, &, >, if, then, else,
----------------------
(“is largest number in the set” is output statement)
----------------------

Writing Language-Neutral Algorithms 63


Notes
2. Remove the words which indicate mathematical or logical computation
----------------------
like increment, compare, etc.
---------------------- Remove - compare, &, >
---------------------- 3. Does the list consist of standard keywords? If yes, then algorithm is
language-neutral.
----------------------
Remaining list is – if, then, else
---------------------- Yes, it consists of standard keywords, hence algorithm is language- neutral.
----------------------
Check your Progress 5
----------------------

---------------------- State True or False.


1. Use of mathematical operators like add, subtract make algorithms
----------------------
language-dependent.
---------------------- 2. We can verify if an algorithm is language-neutral or not.
----------------------

---------------------- Activity 3
----------------------
List the words which indicate mathematical or logical computation.
----------------------

----------------------

----------------------
Summary

---------------------- • Algorithms must be language-neutral to ensure they can be coded in the


most-suited programming language.
---------------------- • Algorithms have input data, steps of instructions and output.
---------------------- • Algorithms contain certain keywords which have predefined meaning.
---------------------- • Complex algorithms should be broken down into simple ones.
• Organisation of statement in appropriate order is important to ensure
----------------------
effectiveness of an algorithm.
---------------------- • We can verify if an algorithm is language-neutral.
----------------------
Keywords
----------------------
• Neutral: Unbiased.
----------------------
• Module: Independent self-contained section.
----------------------
• Heterogeneous: Combination of different types.
---------------------- • Imperative: Giving command to perform some action.

64 Algorithms and Programming Concepts


• Computation: Calculation. Notes

----------------------
Self-Assessment Questions
----------------------
1. Write a short note on components of an algorithm.
2. Why is it important to write language-neutral algorithms? ----------------------

3. Describe use of following keywords in an algorithm with examples: ----------------------


i. If, then, else ----------------------
ii. For
----------------------
4. Write a language-neutral algorithm to arrange three numbers in ascending
order. ----------------------
5. What are the steps to verify the language-neutral nature of an algorithm? ----------------------

----------------------
Answers to Check your Progress
----------------------
Check your Progress 1
State True or False. ----------------------

1. False ----------------------
2. True ----------------------
Check your Progress 2
----------------------
Fill in the blanks.
----------------------
1. Algorithm involves input, steps and output.
2. Complex algorithms can be broken down into simple ones. ----------------------

Check your Progress 3 ----------------------


Fill in the blanks. ----------------------
1. Algorithms should be written in simple English language.
----------------------
2. Indentation should be used to indicate sub-processes in the algorithm.
----------------------
Check your Progress 4
Multiple Choice Multiple Response. ----------------------

1. Which of these should be checked in the organisation of an algorithm? ----------------------


i. Ambiguity ----------------------
iii. Keywords
----------------------
Check your Progress 5
----------------------
State True or False.
1. False ----------------------

2. True ----------------------

Writing Language-Neutral Algorithms 65


Notes
Suggested Reading
----------------------
1. Cormen, Thomas H. Charles E. Leiserson, Ronald L. Rivest and Clifford
---------------------- Stein. Introduction to Algorithms. The MIT Press.
2. Heineman, George T., Gary Pollice and Stanley Selkow. Algorithms in a
----------------------
Nutshell. O’Reilly Media.
---------------------- 3. Knuth, Donald E. Art of Computer Programming, Volume 1: Fundamental
---------------------- Algorithms. Addison-Wesley Professional.

----------------------

----------------------

----------------------

----------------------
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

66 Algorithms and Programming Concepts


Algorithms for Programming Cases
UNIT

5
Structure:

5.1 Introduction
5.2 Sequential Flow
5.3 Random Jump in the Flow
5.4 Conditional Flow
5.5 Repetitive Flow
5.6 Iterative Flow
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Algorithms for Programming Cases 67


Notes
Objectives
----------------------
After going through this unit, you will be able to:
----------------------
l Write simple sequential algorithms
----------------------
l Use go to, if keywords to write algorithm
---------------------- l Identify the need to add condition to an algorithm
---------------------- l Add conditional flows to an algorithm

---------------------- l Write algorithms with repetitive steps


l Explain the order of execution of steps in a simple algorithm
----------------------

---------------------- 5.1 INTRODUCTION


---------------------- In the previous unit, we studied about the basic language format to be
---------------------- used while writing the algorithms. Algorithms must be language-neutral to
ensure they can be coded in the most suitable programming language.
----------------------
We also studied about structure of an algorithm, which consists of input
---------------------- data, steps of instructions and output. Algorithms contain certain keywords
which have predefined meaning. Organisation of instructions in appropriate
---------------------- order is important to ensure effectiveness of an algorithm.
---------------------- After studying the structure and organisation of an algorithm, let us
now study various organisations of instructions in an algorithm. We can break
---------------------- complex algorithms into simple ones and the organisation of various flows
and sequences ensures that the required order is maintained in the execution.
----------------------
Correct organisation of instructions helps to decide the way various statements
---------------------- will be executed to arrive at the result.

---------------------- At this stage, some of the questions we might ask are:


• How does order of various steps in an algorithm matter?
----------------------
• When we decide to write an algorithm, what should be the order of various
---------------------- statements?
---------------------- • What should we do, if we need to repeat certain steps?
• What if there are certain conditions that must be met before the set of
----------------------
steps can be executed?
---------------------- • What should we do if certain steps must be performed before continuing
with regular flow?
----------------------
• How should we decide the order of steps in any situation?
----------------------
In this unit, we will answer these questions and learn the different ways
---------------------- of organising and ordering steps. The order of steps decides the execution
sequence of steps. Let us try to understand this better with help of examples.
----------------------

68 Algorithms and Programming Concepts


5.2 SEQUENTIAL FLOW Notes
When we write an algorithm, multiple steps are written one after the other, ----------------------
in the order in which they should be executed.
----------------------
We use numbering to decide the sequence of steps. Numbering also helps
to identify the step uniquely. ----------------------
For example, in the following algorithm each step is uniquely identified ----------------------
by a number 1, 2, 3 or 4.
----------------------
Step 1: …
Step 2: … ----------------------
Step 3: … ----------------------
Step 4: … ----------------------
Fig. 5.1: Sequential Steps
----------------------
In the above algorithm, first Step 1 will be executed, then Step 2 will be
executed then Step 3 will be executed and then Step 4. Such a flow of execution ----------------------
is called sequential flow.
----------------------
In sequential flow, steps are executed in order of step number starting
from first to last, with no deviations, jumps, repetitions, etc. Order of execution ----------------------
of steps in sequential flow is in ascending order of step number. Hence, it is also
----------------------
called sequential order.
Sequential flow is easiest of all the flows to understand, write and ----------------------
implement. It is also the basic or fundamental flow of steps in any algorithm.
----------------------
Let us write an algorithm now to understand this better.
Example 1: Algorithm to add three numbers A, B, C. ----------------------
In order to add three numbers, we need to first add two numbers ----------------------
Step 1: Add A and B and store the value in D ----------------------
Now add the third number to this
----------------------
Step 2: Add C and D and store the value in D
----------------------
So sample algorithm to add three numbers A, B, C would be as below:
Start ----------------------
Step 1: Add A and B and store the value in D ----------------------
Step 2: Add C and D and store the value in D ----------------------
End
----------------------
In this way we can add three numbers. The steps are executed in a
sequence, first Step 1 and then Step 2. There are no alternate flows or jumps in ----------------------
the flow.
----------------------
Generally, almost every algorithm has certain number of steps which are
executed in a sequence. We can call such set of steps a block. So these steps ----------------------

Algorithms for Programming Cases 69


Notes form a sequential block in an algorithm. Every algorithm has blocks which have
sequential flow.
----------------------
Consider an algorithm which has three blocks or three set of sequential
---------------------- steps.
Block 1
----------------------

----------------------
Block 2
---------------------- …
---------------------- Block 3
---------------------- Fig. 5.2: Sequential Blocks
Block 1 has steps which execute in a sequence. Similarly, Block 2 has
----------------------
steps which execute in a sequence and Block 3 has steps which execute in a
---------------------- sequence. There may be steps between Block 1, Block 2 and Block 3 which
decide the frequency and order of execution of these blocks.
----------------------
We can demarcate the statements to be executed in a block in many ways.
---------------------- Few common practices are explained below:

---------------------- 1. Start block ..End block: Statement like Start Block can be used to mark
the beginning of the block and statement End Block can be used to mark
---------------------- the end of the block. All statements within Start Block and End Block can
form the actual steps to be executed.
----------------------
For example:
----------------------
Start Block
---------------------- Statement 1
---------------------- Statement 2

---------------------- Statement 3
End Block
----------------------
Statement Start Block and End Block do not perform any calculation or
---------------------- activity. All the steps are encapsulated within the Start and End.
---------------------- Similarly, other words can be used like Start-End, Begin-End, etc.
2. Indentation: Indentation or moving the beginning of the steps to right
----------------------
to form next indent alignment, can be used to indicate the steps included
---------------------- inside a block.

---------------------- For example:


Statement 1
----------------------
Statement 2
----------------------
Statement 3
---------------------- Statement 4

70 Algorithms and Programming Concepts


Statement 5 Notes
Statement 6
----------------------
In this example Statement 3, Statement 4 and Statement 5 form a block.
----------------------
We will use the indentation to indicate the block of statements to be
executed. ----------------------
As shown in Figure 5.2 there may be steps between Block 1, Block 2 and ----------------------
Block 3 which impact the frequency and order of execution of the blocks in an
algorithm. In the next sections we will study about these steps which help to ----------------------
change the normal sequential flow into non-sequential.
----------------------
Check your Progress 1 ----------------------

----------------------
Fill in the blanks.
1. Normally the steps in an algorithm are executed in _____________ ----------------------
order.
----------------------
2. Order of execution of steps in sequential flow is in _____order of step
number. ----------------------
3. A set of steps executed in a sequence is called a _______. ----------------------

----------------------
5.3 RANDOM JUMP IN THE FLOW ----------------------
We have learnt that the default order of execution of steps in an algorithm ----------------------
is sequential. However, we may not always want to execute all the steps in a
sequence and may want to jump to some other point in the algorithm. ----------------------
We can use ‘go to’ keyword to jump to a specific step in an algorithm. ----------------------
Go to
----------------------
Whenever we do not want to execute the next sequential step we can use
‘go to’ keyword to decide the next step to be executed. ----------------------
For example ‘go to Step 3’ indicates that the execution should resume ----------------------
from Step 3. That means the next step to be executed would be Step 3. All the
steps in between current step and ‘go to’ step (Step 3 in this case) would be ----------------------
skipped. ----------------------
Let’s consider following case
----------------------
Step 1
----------------------
Step 2
Step 3 …go to Step 6 ----------------------
Step 4 ----------------------
Step 5 ----------------------

Algorithms for Programming Cases 71


Notes Step 6
Step 7
----------------------
In this algorithm the sequence of execution would be:
----------------------
Step 1
---------------------- Step 2
---------------------- Step 3
---------------------- Step 6
Step 7
----------------------
Here, after Step 3, Step 4 would not be executed; instead Step 6 would be
---------------------- executed. Step 4 and Step 5 would be skipped. After Step 6 the steps would be
executed in sequence, unless there is another jump.
----------------------
Let us write an algorithm using ‘go to’.
----------------------
Example 2: Algorithm to find area and volume of a rectangular box.
---------------------- Start
---------------------- Step 1: Get the length of the rectangular box
---------------------- Step 2: Get the breadth of the rectangular box
Step 3: Get the height of the rectangular box
----------------------
Step 4: To find area go to Step 6
----------------------
Step 5: To find volume go to Step 7
---------------------- Step 6: Multiply the length and breadth to get the area of the rectangular
---------------------- base; go to end
Step 7: Multiply length, breadth and height to get the volume of the
----------------------
rectangular box
---------------------- End
---------------------- In this algorithm when we want to find the area of the rectangular base, following
steps would be executed:
----------------------
Step 1: Get the length of the rectangular box
---------------------- Step 2: Get the breadth of the rectangular box
---------------------- Step 3: Get the height of the rectangular box
---------------------- Step 4: To find area go to Step 6
Step 6: Multiply the length and breadth to get the area of the rectangular base;
----------------------
go to end
---------------------- Note that Step 5 and Step 7 are not executed.
---------------------- However, when we want to find the volume, following steps would be executed:
---------------------- Step 1: Get the length of the rectangular box

72 Algorithms and Programming Concepts


Step 2: Get the breadth of the rectangular box Notes
Step 3: Get the height of the rectangular box
----------------------
Step 4: To find area go to Step 6
----------------------
Step 5: To find volume go to Step 7
Step 7: Multiply length, breadth and height to get the volume of the rectangular ----------------------
box ----------------------
Note that Step 6 is not executed.
----------------------
Thus, when we want to change the sequential flow of an algorithm then
we can add random jumps in the algorithm. ----------------------

----------------------
Check your Progress 2
----------------------
State True or False.
----------------------
1. Steps in an algorithm can always be executed sequentially.
----------------------
2. ‘go to’ keyword can be used to jump to a specific step in an algorithm.
----------------------

----------------------
Activity 1
----------------------
Write down the order of execution of steps in the following algorithm: ----------------------
Step 1:
----------------------
Step 2: Go to Step 6
----------------------
Step 3:
Step 4: ----------------------

Step 5: Go to Step 7 ----------------------


Step 6: Go to Step 3 ----------------------
Step 7:
----------------------

----------------------
5.4 CONDITIONAL FLOW
----------------------
We have learnt that the default order of execution of steps in an algorithm
is sequential and we can change this order by adding random jumps. There ----------------------
may be logical conditions when the normal flow should be changed like before
----------------------
executing certain steps we may want to check few conditions. In this section,
we will learn about such cases. ----------------------
When certain set of steps, i.e., blocks are executed based on certain
----------------------
conditions, the algorithm is said to have conditional flows.
----------------------

Algorithms for Programming Cases 73


Notes Some of the cases when we need conditional flows are:
• To check prerequisites
----------------------
• To verify that all the requirements are met
----------------------
• To check the preconditions
---------------------- In an algorithm the condition to be tested is valid if it returns one of the
---------------------- two possible values, say true or false, yes or no, etc.
Some of the valid conditions are:
----------------------
• A is greater than B.
----------------------
• Gender is male.
---------------------- • Number is more than 3.
---------------------- • Bulb is switched on.

---------------------- • The day is Sunday.


We generally use keyword ‘if..else..’ to indicate a conditional flow.
----------------------
The format for writing a conditional flow using ‘if ..else’ is:
----------------------
If <condition is true>
---------------------- Block 1
---------------------- Else

---------------------- Block 2
In a conditional flow, the condition is evaluated and based on the result of
---------------------- the condition, the corresponding block is executed.
---------------------- In the above example:
---------------------- • If the condition is true, then Block 1 will be executed.

---------------------- • If the condition is false, then Block 2 will be executed.


Therefore, either Block 1 or Block 2 is executed but not both.
----------------------
Conditional flow is depicted using the diagram below.
----------------------

----------------------

----------------------
Condition
----------------------

----------------------

---------------------- Fig. 5.3: Conditional Flow


---------------------- We have learnt that all the steps may not be executed in the sequence in
conditional flow.
----------------------

74 Algorithms and Programming Concepts


Let us now write an algorithm with conditional flow. Notes
Example 3: Algorithm to find a leap year.
----------------------
Firstly, ask the user to provide the year to be checked
----------------------
Step 1: Get the 4 digit year number
To find if a year is leap we must divide it by 4 ----------------------

Step 2: Divide the year number by 4, store the remainder in X ----------------------


If the year is divisible by 4 then only it is leap year ----------------------
Step 3: If X is 0 then execute Step 4, else go to Step 5
----------------------
Step 4: The year is leap year
----------------------
Step 5: The year is not leap year
So, an algorithm to find a leap year is ----------------------

Start ----------------------
Step 1: Get the 4 digit year number ----------------------
Step 2: Divide the year number by 4, store the remainder in X
----------------------
Step 3: If X is 0 then execute Step 4, else go to Step 5
----------------------
Step 4: The year is leap year. Go to End.
Step 5: The year is not leap year. ----------------------

End ----------------------
In this algorithm we can see that depending on the result of the condition ----------------------
certain steps may be executed or not.
----------------------
Let us check the execution of leap year algorithm for various scenarios:
Scenario 1: Year = 1980 ----------------------
Step 1: Year = 1980 ----------------------
Step 2: divide 1980 by 4; remainder =0, divisor= 495; X=0 ----------------------
Step 3: X=0
----------------------
(Condition is true, therefore we will execute step 4)
Step 4: 1980 is leap year ----------------------
(The execution with stop, Step 4 has Go to End) ----------------------
Scenario 2: Year = 1991 ----------------------
Step 1: Year = 1991
----------------------
Step 2: divide 1991 by 4; remainder =3, divisor= 497; X=3
Step 3: X=3, hence X is not equal to 0 ----------------------
(Condition is false, therefore we will go to step 5) ----------------------
Step 5: 1991 is not a leap year
----------------------

Algorithms for Programming Cases 75


Notes In above leap year algorithm, depending on value of X, Step 4 or Step 5
is executed but never both.
----------------------
Algorithms can have more than one conditional flow. Let us understand
---------------------- this better with examples.
Example 4: Algorithm to calculate annual amount of interest on loan based
----------------------
on interest rate.
---------------------- Firstly get the loan amount (A). Depending on loan amount get the rate
of interest (R). Then calculate amount of interest (I) by using the formula I =
----------------------
(A*R)/100
---------------------- So the algorithm could be:
---------------------- Start
---------------------- Step 1: Get the loan amount
Step 2: If loan amount is less than 10 lakh then go to Step 3 else go to
----------------------
Step 5.
---------------------- Step 3: Rate of interest is 7% per annum.
---------------------- Step 4: Go to Step 10

---------------------- Step 5: If loan amount between 10 lakh and 20 lakh then go to Step 6 else
go to Step 8
---------------------- Step 6: Rate of interest is 10% per annum.
---------------------- Step 7: Go to Step 10
---------------------- Step 8: If loan amount is more than 20 lakh then go to Step 9 else go to
Step 10
----------------------
Step 9: Rate of interest is 14% per annum.
---------------------- Step 10: Amount of Interest = (loan amount * rate of interest)/100
---------------------- End
---------------------- Note the indentation used to indicate the block of steps to be executed
after if condition. Also, Step 9 need not be followed by ‘Go to Step 10’ because
---------------------- that is the next step in the sequence.
---------------------- Let us check the execution of this algorithm for various scenarios.
Scenario 1: Loan Amount is 7 lakh
----------------------
Step 1: Loan Amount = 7 lakh
----------------------
Step 2: Loan Amount is less than 10 lakh;
---------------------- (Condition is true, so go to Step 3)
---------------------- Step 3: Rate of interest = 7%
---------------------- Step 4: Go to Step 10
Step 10: Amount of Interest = (7 lakh * 7)/100 = 49000
----------------------

76 Algorithms and Programming Concepts


Scenario 2: Loan Amount is 12 lakh Notes
Step 1: Loan Amount = 12 lakh
----------------------
Step 2: Loan Amount is not less than 10 lakh
(Condition is false, so go to Step 5) ----------------------
Step 5: Loan Amount is between10 lakh and 20 lakh ----------------------
(Condition is true, so go to Step 6)
----------------------
Step 3: Rate of interest = 10%
----------------------
Step 4: Go to Step 10
Step 10: Amount of Interest = (12 lakh * 10)/100 = 120000 ----------------------
Scenario 3: Loan Amount is 22 lakh ----------------------
Step 1: Loan Amount = 22 lakh
----------------------
Step 2: Loan Amount is not less than 10 lakh
(Condition is false, so go to Step 5) ----------------------
Step 5: Loan Amount is not between10 lakh and 20 lakh ----------------------
(Condition is false, so go to Step 8)
----------------------
Step 8: Loan Amount is more than 20 lakh
----------------------
(Condition is true, so go to Step 9)
Step 9: Rate of interest = 14% ----------------------
Step 10: Amount of Interest = (22 lakh * 14)/100 = 3080000 ----------------------

Check your Progress 3 ----------------------

----------------------
Multiple Choice Single Response.
1. Block following a condition is executed: ----------------------
i. If the condition is true ----------------------
ii. If the condition is false ----------------------
iii. If the condition has no result
----------------------
Multiple Choice Multiple Response.
----------------------
1. A condition must return result of following types:
i. True ----------------------
ii. False ----------------------
iii. No result ----------------------

----------------------
Activity 2
----------------------
Write an algorithm to check if the person is eligible to vote. ----------------------

Algorithms for Programming Cases 77


Notes 5.5 REPETITIVE FLOW
---------------------- We have learnt that depending on the value of conditions, the order of
execution of steps may change. The conditional flow helps us to decide which
---------------------- steps to execute under scenario. How will we write algorithm if we have to
execute same step again and again under certain condition? Let us learn to do
----------------------
that.
---------------------- Consider a case where we have to repeat a certain set of steps. In this case
---------------------- we can go back to the step from where repetition should start.
Step 1
----------------------
Step 2
----------------------
Step 3
---------------------- Step 4
---------------------- Step 5… go to Step 2

---------------------- Step 6
In this case after Step 5, again Step 2 would be executed, then Step 3,
---------------------- Step 4, Step 5, again Step 2, and so on. However, to ensure this does not go on
---------------------- endlessly, we must exit out of block by checking a condition in Step 2, 3, 4 or
5 and then moving execution to Step 6 or elsewhere. In this case, we will be
---------------------- repeatedly execute Step 2 to Step 5. Such kind of flow is called repetitive flow.
Repetitive flow can be represented using the circular arrow symbol as in the
---------------------- figure below.
----------------------

----------------------

----------------------

----------------------

---------------------- Fig. 5.4: Repetitive Flow


---------------------- In this repetitive flow Step 3, Step 4 and Step 5 are repeated. After Step
5, Step 3 is executed again. The repetition can continue until certain condition
---------------------- is true or condition becomes true. So there are two formats for checking the
---------------------- condition to exit from repetitive flow.
Format 1: Test exit condition first
----------------------
Test Condition
----------------------
If true
---------------------- Repeat block
---------------------- When false

---------------------- Next steps

78 Algorithms and Programming Concepts


Format 2: Test exit condition later Notes
Repeat block
----------------------
Test Condition
If true ----------------------
Next Step ----------------------
When false
----------------------
Go back to Repeat block
Let us understand this better with examples. ----------------------
Example 5: Algorithm to cut all apples from a basket. ----------------------
We need to get apples from the basket one by one and cut them before
serving them all. ----------------------
Start ----------------------
Step 1: Get the basket of apples
----------------------
Step 2: If there are more apples in the basket then go to Step 3 else go to
Step 7 ----------------------
Step 3: Pick the apple
----------------------
Step 4: Wash the apple
Step 5: Cut the apple ----------------------
Step 6: Go to Step 2 ----------------------
Step 7: Arrange all cut apples in a plate and serve.
----------------------
End
In this example we will repeat Step 3, Step 4 and Step 5 for all the apples ----------------------
in the basket. ----------------------
Scenario 1: When there are three apples in the basket
----------------------
Step 1: Get the basket of apples
Step 2: There are more apples in the basket ----------------------
(Condition is true so go to step 3) ----------------------
Step 3: Pick the first apple
----------------------
Step 4: Wash the first apple
Step 5: Cut the first apple ----------------------
Step 6: Go to Step 2 ----------------------
Step 2: There are more apples in the basket
----------------------
(Condition is true so go to step 3)
Step 3: Pick the first apple ----------------------
Step 4: Wash the first apple ----------------------
Step 5: Cut the first apple
----------------------
Step 6: Go to Step 2
Step 2: There are more apples in the basket ----------------------

Algorithms for Programming Cases 79


Notes (Condition is true so go to step 3)
Step 3: Pick the first apple
----------------------
Step 4: Wash the first apple
---------------------- Step 5: Cut the first apple
---------------------- Step 6: Go to Step 2
Step 2: There are no more apples in the basket
----------------------
(Condition is false so go to step 7)
---------------------- Step 7: Arrange all cut apples in a plate and serve.
---------------------- Note the following about repetitive flow:
• There must be a condition which needs to be fulfilled to come out of
---------------------- repetition.
---------------------- • The changes in the loop should be such that the progress is made towards
reaching the condition.
----------------------
Imagine a case where the condition is “if there are more apples in the
---------------------- basket” and in the repetition block we have a step “add more apple to the
basket”. Will the repetition ever end? No. So we must progress towards meeting
---------------------- the condition.
---------------------- Now let us check some mathematical examples. Let us write an algorithm
to check if a number is prime or not. If we revise the definition of prime number,
---------------------- it is a natural number which is divisible by only 1 and itself and no other number.
Example 6: Algorithm to check if a number is a prime number.
----------------------
Start
---------------------- Step 1: Get the number into variable N
---------------------- Let’s take another variable X to store that number by which we will divide
N. To check prime number we must start divisibility check from number 2.
---------------------- (Every natural number is divisible by 1)
---------------------- Step 2: Let X=2
We need to check divisibility only till half the value of N. If a number is
----------------------
not divisible by any number between 2 and N/2 then it is prime.
---------------------- Step 3: If X is greater than or equal to N/2 then go to Step 8 else go to
Step 4
----------------------
Step 4: Divide N by X
---------------------- Step 5: If remainder is 0 then go to Step 9 else go to Step 6
---------------------- Step 6: Increment X by 1
Step 7: Go to Step 3
----------------------
Step 8: The number is a prime number, Go to End
---------------------- Step 9: The number is not a prime number
---------------------- End
Let us check few scenarios for this algorithm
----------------------

80 Algorithms and Programming Concepts


Scenario 1: Number is 6 Notes
Step 1: N=6
----------------------
Step 2: Let X=2
Step 3: X (i.e., 2) is not greater than or equal to N/2 (N/2 = 3) ----------------------
Step 4: Divide N by X; (6 divided by 2, divisor =3, remainder=0) ----------------------
Step 5: Remainder =0
----------------------
(Condition is true hence go to Step 9)
Step 9: The number is not a prime number. ----------------------
Scenario 2: Number is 13 ----------------------
Step 1: N=13
----------------------
Step 2: Let X=2
Step 3: X (i.e., 2) is not greater than or equal to N/2 (N/2 = 6.5) ----------------------
Step 4: Divide N by X; (13 divided by 2, divisor =6, remainder=1) ----------------------
Step 5: Remainder =1
----------------------
Step 6: Increment X by 1; X=3
Step 7: Go to Step 3 ----------------------
Step 3: X (i.e., 3) is not greater than or equal to N/2 (i.e., 6.5) ----------------------
Step 4: Divide N by X; (13 divided by 3, divisor =4, remainder=1)
----------------------
Step 5: Remainder =1
Step 6: Increment X by 1; X=4 ----------------------
Step 7: Go to Step 3 ----------------------
Step 3: X (i.e., 4) is not greater than or equal to N/2 (i.e., 6.5)
----------------------
Step 4: Divide N by X; (13 divided by 4, divisor =3, remainder=1)
Step 5: Remainder =1 ----------------------
Step 6: Increment X by 1; X=5 ----------------------
Step 7: Go to Step 3
----------------------
Step 3: X (i.e., 5) is not greater than or equal to N/2 (i.e., 6.5)
Step 4: Divide N by X; (13 divided by 5, divisor =2, remainder=3) ----------------------
Step 5: Remainder =3 ----------------------
Step 6: Increment X by 1; X=6
----------------------
Step 7: Go to Step 3
Step 3: X (i.e., 6) is not greater than or equal to N/2 (i.e., 6.5) ----------------------
Step 4: Divide N by X; (13 divided by 6, divisor =2, remainder=1) ----------------------
Step 5: Remainder =1;
Step 6: Increment X by 1; X=7 ----------------------
Step 7: Go to Step 3 ----------------------
Step 3: X (i.e., 7) is greater than or equal to N/2 (i.e., 6.5)
----------------------

Algorithms for Programming Cases 81


Notes (Condition is true hence go to Step 8)
Step 8: The number is a prime number.
----------------------
Now let us write an algorithm to test condition later.
---------------------- Example 7: Algorithm to find a matching pair of socks from a bag.
---------------------- We will take out one socks first and then take other socks from the bag,
one at a time, until we find a match for the first one.
---------------------- Start
---------------------- Step 1: Take out first socks from the bag, note its color
Step 2: Take out another socks from the bag, note its color
----------------------
Step 3: If the color of this socks match the first socks then go to step 4
---------------------- else go to Step 2
---------------------- Step 4: Found matching pair of the socks
End
----------------------
In this algorithm, Step 2 and Step 3 will be repeated till the condition is
---------------------- fulfilled. Thus, in this case there will be at least one execution of the steps that
are repeated (Step 2 and Step 3).
---------------------- Thus, we should use format 2 (check condition before repetition) when
---------------------- the steps in the repletion block must be executed at least once. Otherwise, we
can use format 1, where the steps in the repetition block may not be executed
---------------------- at all.

---------------------- Check your Progress 4


----------------------
State True or False.
----------------------
1. Indentation can be used to indicate block of statements.
----------------------
2. The value of condition must be changed in the repetitive loop.
----------------------

----------------------
Activity 3
----------------------
Write an algorithm to find the largest of a set of five numbers.
----------------------

----------------------
5.6 ITERATIVE FLOW
----------------------
Till now we have learnt to execute certain statements in algorithm
----------------------
depending on conditions and also repeat the statement conditionally. Sometimes
---------------------- we may have to repeat set of statement fixed number of times, i.e., iterate over
the statements certain number of times. These types of flows where we iterate
---------------------- over the block of steps are called iterative flows.
----------------------

82 Algorithms and Programming Concepts


Iterative algorithms are a special type of repetitive algorithm, which meet Notes
the following three conditions:
----------------------
1. The variable to be checked in the condition is set to some initial value.
2. Condition checks the value of the variable. ----------------------
3. The repetition loop changes the value of the variable used in the condition. ----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
----------------------

----------------------

----------------------
Fig. 5.5: Iterative Flow
----------------------
Let us understand this better with some examples.
----------------------
Example 8: Algorithm to add first n numbers.
To add first n numbers, we must add 1 and 2 first. Then add 3 to this sum ----------------------
and continue till we have added n to the sum. So the algorithm could be: ----------------------
Start
----------------------
Step 1: X = 1
----------------------
Step 2: Y = 2
Step 3: If Y = n+1 then go to Step 7 else go to Step 4 ----------------------

Step 4: X =X+Y ----------------------


Step 5: Y = Y+1 ----------------------
Step 6: Go to Step 3
----------------------
Step 7: Sum of the first n numbers is X
----------------------
End
Now let us check if this flow met the 3 conditions listed above for an ----------------------
iterative flow. ----------------------
1. The variable to be checked in the condition is set to some initial value.
----------------------

----------------------

Algorithms for Programming Cases 83


Notes The variable Y is set to some initial value, i.e., 2.
2. Condition checks the value of the variable.
----------------------
In this algorithm the condition checks the value of the variable Y.
----------------------
3. The repetition loop changes the value of the variable used in the condition.
---------------------- Value if Y is incremented in the loop until it becomes more than 10.
---------------------- Let us see how this algorithm will work.
---------------------- Scenario 1: n=4
Step 1: X = 1
----------------------
Step 2: Y = 2
----------------------
Step 3: Y (i.e., 2) is not equal to n+1 (i.e., 4+1=5)
---------------------- (Condition is false so go to Step 3)
---------------------- Step 4: X =1+2 =3
---------------------- Step 5: Y = 2+1 =3
Step 6: Go to Step 3
----------------------
Step 3: Y (i.e., 3) is not equal to 5
----------------------
(Condition is false so go to Step 3)
---------------------- Step 4: X =3+3 =6
---------------------- Step 5: Y = 3+1 =4
---------------------- Step 6: Go to Step 3
Step 3: Y (i.e., 4) is not equal to 5
----------------------
(Condition is false so go to Step 3)
----------------------
Step 4: X =6+4 =10
---------------------- Step 5: Y = 4+1 =5
---------------------- Step 6: Go to Step 3
---------------------- Step 3: Y (i.e., 11) is equal to 5
(Condition is true so go to Step 7)
----------------------
Step 7: Sum of the first 4 numbers is 10
----------------------
Scenario 2: n=10
---------------------- Step 1: X = 1
---------------------- Step 2: Y = 2
---------------------- Step 3: Y (i.e., 2) is not equal to 11
(Condition is false so go to Step 3)
----------------------
Step 4: X =1+2 =3
----------------------

84 Algorithms and Programming Concepts


Step 5: Y = 2+1 =3 Notes
Step 6: Go to Step 3
----------------------
Step 3: Y (i.e., 3) is not equal to 11
----------------------
(Condition is false so go to Step 3)
Step 4: X =3+3 =6 ----------------------

Step 5: Y = 3+1 =4 ----------------------


Step 6: Go to Step 3 ----------------------
Step 3: Y (i.e., 4) is not equal to 11
----------------------
(Condition is false so go to Step 3)
----------------------
Step 4: X =6+4 =10
Step 5: Y = 4+1 =5 ----------------------

Step 6: Go to Step 3 ----------------------


Step 3: Y (i.e., 5) is not equal to 11 ----------------------
(Condition is false so go to Step 3)
----------------------
Step 4: X =10+5 =15
----------------------
Step 5: Y = 5+1 =6
Step 6: Go to Step 3 ----------------------

Step 3: Y (i.e., 6) s not equal to 11 ----------------------


(Condition is false so go to Step 3) ----------------------
Step 4: X =15+6 =21
----------------------
Step 5: Y = 6+1 =7
----------------------
Step 6: Go to Step 3
Step 3: Y (i.e., 7) is not equal to 11 ----------------------

(Condition is false so go to Step 3) ----------------------


Step 4: X =21+7 =28 ----------------------
Step 5: Y = 7+1 =8
----------------------
Step 6: Go to Step 3
----------------------
Step 3: Y (i.e., 8) is not equal to 11
(Condition is false so go to Step 3) ----------------------

Step 4: X =28+8 =36 ----------------------


Step 5: Y = 8+1 =9 ----------------------
Step 6: Go to Step 3
----------------------

----------------------

Algorithms for Programming Cases 85


Notes Step 3: Y (i.e., 9) is not equal to 11
(Condition is false so go to Step 3)
----------------------
Step 4: X =36+9 =45
----------------------
Step 5: Y = 9+1 =10
---------------------- Step 6: Go to Step 3
---------------------- Step 3: Y (i.e., 10) is not equal to 11
---------------------- (Condition is false so go to Step 3)
Step 4: X =45+10 =55
----------------------
Step 5: Y = 10+1 =11
----------------------
Step 6: Go to Step 3
---------------------- Step 3: Y (i.e., 11) is equal to 11
---------------------- (Condition is true so go to Step 7)
---------------------- Step 7: Sum of the first 10 numbers is 55
Thus, we can decide the number of times the steps in an algorithm should
----------------------
be executed. Sometimes the number of times the block would be iterated
---------------------- depends on variable values.

---------------------- Check your Progress 5


----------------------
Multiple Choice Multiple Response.
----------------------
1. Iterative algorithms must meet the following conditions:
----------------------
i. The variable to be checked in the condition is set to some initial
---------------------- value.
ii. Condition checks the value of the variable.
----------------------
iii. All the statements must execute in a sequence.
----------------------
iv. The repetition loop changes the value of the variable used in the
---------------------- condition.

---------------------- v. No statement should be executed again.

----------------------

----------------------
Summary

---------------------- • Algorithms contain steps which are by default executed in sequential


order.
---------------------- • Sequence numbers are used to identify each step in an algorithm.
---------------------- • Using ‘go to’ keyword the order of execution of an algorithm can be
modified.
----------------------

86 Algorithms and Programming Concepts


• Whenever certain steps must be executed based on a precondition then Notes
condition must be used before the set of steps.
----------------------
• The steps following a condition are executed only if the result of condition
is positive – true, yes, etc. ----------------------
• If the result of condition is negative we must provide the step to be
----------------------
followed in the algorithm.
• Whenever same steps need to be repeated, instead of writing the steps ----------------------
again, ‘go to’ can be used to repeat the steps.
----------------------
• In repetitive and iterative flows there must be an exit condition.
----------------------
Keywords ----------------------

• Sequential: Following in a logical order or sequence. ----------------------


• Frequency: Number of occurrences. ----------------------
• Conditional: Subject to one or more conditions or requirements being
----------------------
met.
• Variable: One that has varied value. ----------------------

• Repetitive: Containing or characterised by repetition. ----------------------


• Iterative: The act of repeating a process with the aim of approaching a ----------------------
desired goal.
----------------------
Self-Assessment Questions ----------------------
1. Write a short note on conditional flow in an algorithm. ----------------------
2. Describe the following with example: ----------------------
i. Repetitive flow
----------------------
ii. Iterative flow
----------------------
3. Write a simple algorithm to check if the given day is a weekend.
4. Write a simple algorithm to find factorial of a number. ----------------------
5. Write a simple algorithm to compute compound interest. ----------------------

----------------------
Answers to Check your Progress
----------------------
Check your Progress 1
Fill in the blanks. ----------------------

1. Normally the steps in an algorithm are executed in sequential order. ----------------------


2. Order of execution of steps in sequential flow is in ascending order of step ----------------------
number.
----------------------
3. A set of steps executed in a sequence is called a block.

Algorithms for Programming Cases 87


Notes Check your Progress 2
State True or False.
----------------------
1. False
----------------------
2. True
---------------------- Check your Progress 3
---------------------- Multiple Choice Single Response.

---------------------- 1. Block following a condition is executed:


i. If the condition is true
----------------------
Multiple Choice Multiple Response.
----------------------
1. A condition must return result of following types:
---------------------- i. True
---------------------- ii. False
---------------------- Check your Progress 4
State True or False.
----------------------
1. True
----------------------
2. True
---------------------- Check your Progress 5
---------------------- Multiple Choice Multiple Response.
---------------------- 1. Iterative algorithms must meet the following conditions:
i. The variable to be checked in the condition is set to some initial
----------------------
value.
---------------------- ii. Condition checks the value of the variable.
---------------------- iv. The repetition loop changes the value of the variable used in the
condition.
----------------------

---------------------- Suggested Reading


---------------------- 1. Cormen, Thomas H. Charles E. Leiserson, Ronald L. Rivest and Clifford
Stein. Introduction to Algorithms. The MIT Press.
----------------------
2. Heineman, George T., Gary Pollice and Stanley Selkow. Algorithms in a
---------------------- Nutshell. O’Reilly Media.
---------------------- 3. Knuth, Donald E. Art of Computer Programming, Volume 1: Fundamental
Algorithms. Addison-Wesley Professional.
----------------------

----------------------

----------------------

88 Algorithms and Programming Concepts


Advanced Constructs using Algorithms
UNIT

6
Structure:

6.1 Introduction
6.2 Nested Sequences
6.3 Parallel Execution
6.4 Fork and Join
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Advanced Constructs using Algorithms 89


Notes
Objectives
----------------------
After going through this unit, you will be able to:
----------------------
l Write algorithms in nested form
----------------------
l Identify the flow of statements in a nested algorithm
---------------------- l Use fork and join constructs
---------------------- l Write independent statements to execute in an algorithm

----------------------

---------------------- 6.1 INTRODUCTION


---------------------- In the previous unit, we studied about various organisations of instructions
in an algorithm. Algorithms contain steps which are by default executed in
---------------------- sequential order. We can use preconditions to decide if certain set of steps
---------------------- should not be executed unless the condition is fulfilled.
We also studied the use of keywords like ‘go to’ to alter the order of
----------------------
execution of an algorithm. Whenever same set of steps need to be repeated,
---------------------- instead of writing the steps again, repetitive and iterative flows can be used
to repeat the steps. For repetitive and iterative flows there must be an exit
---------------------- condition. In all these cases the steps are executed in a sequence.
---------------------- After studying the various ways to order the steps of an algorithm, let us
now study about cases where we might need to execute set of steps in parallel.
----------------------
We commonly hear the term multitasking. It is generally used to indicate
---------------------- when multiple tasks or actions are performed simultaneously. Multitasking is
considered to be better performing, efficient and time-effective. Please note
---------------------- that we are not discussing the operating system or program multitasking and
exploring details of how it is managed. We want to identify the situations when
----------------------
we need to perform activities simultaneously and learn to write algorithms to
---------------------- deal with these situations.

---------------------- In this unit, we will learn about the advanced constructs in an algorithm
which can be used to deal with complex situations.
----------------------
6.2 NESTED SEQUENCES
----------------------
In an algorithm multiple steps are written one after the other, in the order
----------------------
in which they should be executed. We can jump to specific statements if that
---------------------- needs to be executed next. Unless a jump is introduced the algorithm continues
in sequence.
----------------------
If we need to repeat certain steps, we jump back to a previous step number.
---------------------- In Algorithm Example 6 in Unit 5, we had repeated steps to check if the given
number is a prime number. The algorithm is copied again here.
----------------------

90 Algorithms and Programming Concepts


Start Notes
Step 1: Get the number into variable N
----------------------
Let’s take another variable X to store that number by which we will divide
N. To check prime number we must start divisibility check from number 2. ----------------------
(Every natural number is divisible by 1)
----------------------
Step 2: Let X=2
----------------------
We need to check divisibility only till half the value of N. If a number is
not divisible by any number between 2 and N/2 then it is prime. ----------------------
Step 3: If X is greater than or equal to N/2 then go to Step 8 else go to ----------------------
Step 4
Step 4: Divide N by X ----------------------

Step 5: If remainder is 0 then go to Step 9 else go to Step 6 ----------------------


Step 6: Increment X by 1 ----------------------
Step 7: Go to Step 3 ----------------------
Step 8: The number is a prime number, Go to End
----------------------
Step 9: The number is not a prime number
----------------------
End
If we observer we go back to Step 3 in Step 7. So the execution repeats ----------------------
itself.
----------------------
We might have cases where one set of steps must be executed within other
set of steps. This type of execution is called nested execution or nested flow. ----------------------
In a nested flow, the body of one set of instructions contains another set ----------------------
of instructions. Whenever we need to repeat a set of statements within another
set of repetitive statements we have to use nested flow. ----------------------

The diagram below depicts a nested repetitive flow, where the outer ----------------------
repetitive loop contains an inner repetitive loop.
----------------------

----------------------

----------------------

----------------------

Condition 2
----------------------
Step 5 ----------------------
Step 6
----------------------

----------------------

Fig. 6.1: Nested Repetitive Flow ----------------------

Advanced Constructs using Algorithms 91


Notes In this figure, the order of execution would be:
Condition 1
----------------------
Steps 3
----------------------
Step 4: Condition 2
---------------------- Step 5
---------------------- Step 6
---------------------- Go to check Condition 2
Step 7
----------------------
Go to Condition 1
----------------------
Step 4 may repeat until condition 2 exits and for every execution of main block
---------------------- Step 4 may repeat numerous times. Let us understand this with some scenarios.

---------------------- Scenario 1
Condition 1: false {Outer loop: 1}
----------------------
Step 3
----------------------
Step 4: Condition 2: false {Outer loop: 1, Inner loop: 1}
---------------------- Step 5
---------------------- Step 6

---------------------- Step 4: Condition 2: false {Outer loop: 1, Inner loop: 2}


Step 5
----------------------
Step 6
----------------------
Step 4: Condition 2: true
---------------------- Step 7
---------------------- Condition 1: false {Outer loop: 2}

---------------------- Step 3
Step 4: Condition 2: false {Outer loop: 2, Inner loop: 1}
----------------------
Step 5
----------------------
Step 6
---------------------- Step 4: Condition 2: false {Outer loop: 2, Inner loop: 2}
---------------------- Step 5
---------------------- Step 6
Step 4: Condition 2: false {Outer loop: 2, Inner loop: 3}
----------------------
Step 5
----------------------
Step 6
---------------------- Step 4: Condition 2: true

92 Algorithms and Programming Concepts


Step 7 Notes
Condition 1: true
----------------------
In this scenario the outer loop executes two times and inner loop executes two
times in the first execution and three times in the second execution. Now let us ----------------------
check another scenario.
----------------------

----------------------
Scenario 2
Condition 1: false {Outer loop: 1} ----------------------

Step 3 ----------------------
Step 4: Condition 2: false {Outer loop: 1, Inner loop: 1} ----------------------
Step 5
----------------------
Step 6
----------------------
Step 4: Condition 2: false {Outer loop: 1, Inner loop: 2}
Step 5 ----------------------

Step 6 ----------------------
Step 4: Condition 2: false {Outer loop: 1, Inner loop: 3} ----------------------
Step 5
----------------------
Step 6
----------------------
Step 4: Condition 2: false {Outer loop: 1, Inner loop: 4}
Step 5 ----------------------

Step 6 ----------------------
Step 4: Condition 2: true ----------------------
Step 7 ----------------------
Condition 1: false {Outer loop: 2}
----------------------
Step 3
----------------------
Step 4: Condition 2: false {Outer loop: 2, Inner loop: 1}
Step 5 ----------------------
Step 6 ----------------------
Step 4: Condition 2: false {Outer loop: 2, Inner loop: 2} ----------------------
Step 5
----------------------
Step 6
----------------------
Step 4: Condition 2: true
Step 7 ----------------------
Condition 1: false {Outer loop: 3} ----------------------

Advanced Constructs using Algorithms 93


Notes Step 3
Step 4: Condition 2: false {Outer loop: 3, Inner loop: 1}
----------------------
Step 5
----------------------
Step 6
---------------------- Step 4: Condition 2: false {Outer loop: 3, Inner loop: 2}
---------------------- Step 5
---------------------- Step 6
Step 4: Condition 2: false {Outer loop: 3, Inner loop: 3}
----------------------
Step 5
----------------------
Step 6
---------------------- Step 4: Condition 2: true
---------------------- Step 7
---------------------- Condition 1: true
In this scenario the outer loop executes three times and the inner loop
----------------------
executes four times in the first execution, two times in second execution and
---------------------- three times in the third execution.
Thus, for every time the outer loop executes, the inner loop starts from
----------------------
beginning and can repeat independently of previous or next execution.
---------------------- Example 1: Algorithm to generate multiplication tables from X to Y.
---------------------- For generating multiplication tables for a number it must be multiplied
with 1 to 10. For every such number the loop of multiplication with 1 to 10 must
---------------------- be repeated.
---------------------- Start
---------------------- Step 1: Get the number X and Y
Step 2: If X=Y+1 then go to Step 11 else go to Step 3
----------------------
Step 3: Set N=1
----------------------
Step 4: If N=11 then go to Step 8 else go to Step 5
---------------------- Step 5: Print X*N
---------------------- Step 6: N= N+1
---------------------- Step 7: Go to Step 4
Step 8: X= X+1
----------------------
Step 9: Move to next line (To start printing next table on next line)
----------------------
Step 10: Go to Step 2
---------------------- Step 11: Stop
----------------------

94 Algorithms and Programming Concepts


In this algorithm Step 4 to Step 6 is inner loop which is repeated 10 times for Notes
every value of X between X and Y. Let us check how the algorithm executes
with actual values. ----------------------
Scenario 1: Generate multiplication tables from 11 to 15. ----------------------
Start
----------------------
Step 1: X=11, Y =15
----------------------
Step 2: X is not equal to Y+1 (X=11, Y=15)
Step 3: Set N=1 ----------------------

Step 4: N is not equal to 11 (N=1) ----------------------


Step 5: Print X*N; (X=11, N=1); Print 11 ----------------------
Step 6: N= N+1; (N=2)
----------------------
Step 7: Go to Step 4
----------------------
Step 4: N is not equal to 11 (N=2)
Step 5: Print X*N; (X=11, N=2); Print 22 ----------------------

Step 6: N= N+1; (N=3) ----------------------


Step 7: Go to Step 4 ----------------------
Step 4: N is not equal to 11 (N=3)
----------------------
Step 5: Print X*N; (X=11, N=3); Print 33
----------------------
Step 6: N= N+1; (N=4)
Step 7: Go to Step 4 ----------------------

Step 4: N is not equal to 11 (N=4) ----------------------


Step 5: Print X*N; (X=11, N=4); Print 44 ----------------------
Step 6: N= N+1; (N=5) ----------------------
Step 7: Go to Step 4
----------------------
Step 4: N is not equal to 11 (N=5)
----------------------
Step 5: Print X*N; (X=11, N=5); Print 55
Step 6: N= N+1; (N=6) ----------------------
Step 7: Go to Step 4 ----------------------
Step 4: N is not equal to 11 (N=6) ----------------------
Step 5: Print X*N; (X=11, N=6); Print 66
----------------------
Step 6: N= N+1; (N=7)
----------------------
Step 7: Go to Step 4
Step 4: N is not equal to 11 (N=7) ----------------------
Step 5: Print X*N; (X=11, N=7); Print 77 ----------------------

Advanced Constructs using Algorithms 95


Notes Step 6: N= N+1; (N=8)
Step 7: Go to Step 4
----------------------
Step 4: N is not equal to 11 (N=8)
----------------------
Step 5: Print X*N; (X=11, N=8); Print 88
---------------------- Step 6: N= N+1; (N=9)
---------------------- Step 7: Go to Step 4
---------------------- Step 4: N is not equal to 11 (N=9)
Step 5: Print X*N; (X=11, N=9); Print 99
----------------------
Step 6: N= N+1; (N=10)
----------------------
Step 7: Go to Step 4
---------------------- Step 4: N is not equal to 11 (N=10)
---------------------- Step 5: Print X*N; (X=11, N=10); Print 110
---------------------- Step 6: N= N+1; (N=11)
Step 7: Go to Step 4
----------------------
Step 4: N is equal to 11 (N=11) Go to Step 8
----------------------
Step 8: X= X+1 (X=12)
---------------------- Step 8: Move to next line (To start printing next table on next line)
---------------------- Step 9: Go to Step 2
---------------------- Step 2: X is not equal to Y+1 (X=12, Y=15)
Step 3: Set N=1
----------------------
Step 4: N is not equal to 11 (N=1)
----------------------
Step 5: Print X*N; (X=12, N=1); Print 12
---------------------- Step 6: N= N+1; (N=2)
---------------------- Step 7: Go to Step 4
---------------------- Step 4: N is not equal to 11 (N=2)
Step 5: Print X*N; (X=12, N=2); Print 24
----------------------
Step 6: N= N+1; (N=3)
----------------------
Step 7: Go to Step 4
---------------------- Step 4: N is not equal to 11 (N=3)
---------------------- Step 5: Print X*N; (X=12, N=3); Print 36
---------------------- Step 6: N= N+1; (N=4)
Step 7: Go to Step 4
----------------------
Step 4: N is not equal to 11 (N=4)
----------------------

96 Algorithms and Programming Concepts


Step 5: Print X*N; (X=14, N=4); Print 48 Notes
Step 6: N= N+1; (N=5)
----------------------
Step 7: Go to Step 4
----------------------
Step 4: N is not equal to 11 (N=5)
Step 5: Print X*N; (X=12, N=5); Print 60 ----------------------

Step 6: N= N+1; (N=6) ----------------------


Step 7: Go to Step 4 ----------------------
Step 4: N is not equal to 11 (N=6)
----------------------
Step 5: Print X*N; (X=12, N=6); Print 72
----------------------
Step 6: N= N+1; (N=7)
Step 7: Go to Step 4 ----------------------

Step 4: N is not equal to 11 (N=7) ----------------------


Step 5: Print X*N; (X=12, N=7); Print 84 ----------------------
Step 6: N= N+1; (N=8)
----------------------
Step 7: Go to Step 4
----------------------
Step 4: N is not equal to 11 (N=8)
Step 5: Print X*N; (X=12, N=8); Print 96 ----------------------

Step 6: N= N+1; (N=9) ----------------------


Step 7: Go to Step 4 ----------------------
Step 4: N is not equal to 11 (N=9)
----------------------
Step 5: Print X*N; (X=12, N=9); Print 108
----------------------
Step 6: N= N+1; (N=10)
Step 7: Go to Step 4 ----------------------

Step 4: N is not equal to 11 (N=10) ----------------------


Step 5: Print X*N; (X=12, N=10); Print 120 ----------------------
Step 6: N= N+1; (N=11)
----------------------
Step 7: Go to Step 4
----------------------
Step 4: N is equal to 11 (N=11) Go to Step 8
Step 8: X= X+1 (X=13) ----------------------

Step 8: Move to next line (To start printing next table on next line) ----------------------
Step 9: Go to Step 2 ----------------------
Step 2: X is not equal to Y+1 (X=13, Y=15)
----------------------
…Algorithm thus continues to print multiplication tables of 13, 14 and 15
----------------------

Advanced Constructs using Algorithms 97


Notes Step 8: X= X+1 (X=16)
Step 8: Move to next line (To start printing next table on next line)
----------------------
Step 9: Go to Step 2
----------------------
Step 2: X is equal to Y+1 (X=16, Y=15) Go to Step 11
---------------------- Step 11: Stop
---------------------- There can be special type of algorithms which need to call itself
repetitively. This is required in situations where we repeat the same logic again
---------------------- and again to arrive at the solution. It is like calling the same function with new
---------------------- set of values. This type of nested flow is also referred to as recursive flow. We
may need to use recursive flow to find factorials of number.
---------------------- Example 2: Find factorial of a positive number.
---------------------- Step 1: Start
---------------------- Step 2: Get the number N

---------------------- Step 3: If N = 1 then go to Step 4 else go to Step 5


Step 4: Factorial (N) = N * Factorial (N-1)
----------------------
Step 5: Factorial (1) = 1
----------------------
Step 6: Print Factorial (N)
---------------------- Step 7: Stop
---------------------- Here at Step 4 same Step is called again. Let us now try to understand the flow
using a scenario.
----------------------
Scenario 1: N =5
---------------------- Step 1: Start
---------------------- Step 2: N=5
---------------------- Step 3: If N = 1 is false, go to Step 4
Step 4: Factorial (5) = 5 * Factorial (4)
----------------------
How do we find factorial of 4? We need to repeat the same step with N=4
----------------------
Step 4: Factorial (4) = 4 * Factorial (3)
---------------------- Step 4: Factorial (3) = 3 * Factorial (2)
---------------------- Step 4: Factorial (2) = 2 * Factorial (1)
---------------------- Now N=1, so we need to exit the loop and go to Step 5
Step 5: Factorial (1) = 1
----------------------
Step 6: Print Factorial (5)
----------------------
If we compute the statements Factorial (5) = 5* 4*3*2*1 = 120
---------------------- Step 7: Stop
---------------------- Scenario 2: N =8

98 Algorithms and Programming Concepts


Step 1: Start Notes
Step 2: N=8
----------------------
Step 3: If N = 1 is false, go to Step 4
----------------------
Step 4: Factorial (8) = 8 * Factorial (7)
Step 4: Factorial (7) = 7 * Factorial (6) ----------------------

Step 4: Factorial (6) = 6 * Factorial (5) ----------------------


Step 4: Factorial (5) = 5 * Factorial (4) ----------------------
Step 4: Factorial (4) = 4 * Factorial (3)
----------------------
Step 4: Factorial (3) = 3 * Factorial (2)
----------------------
Step 4: Factorial (2) = 2 * Factorial (1)
Now N=1, so we need to exit the loop and go to Step 5 ----------------------

Step 5: Factorial (1) = 1 ----------------------


Step 6: Print Factorial (8) ----------------------
If we compute the statements Factorial (8) = 8*7*6*5* 4*3*2*1 = 40320
----------------------
Step 7: Stop
----------------------
Similarly, nested loops can be used to print the number triangles or
sequences like the one below: ----------------------
1 ----------------------
121
----------------------
12321
----------------------
1234321
123454321 ----------------------
12345654321 ----------------------

----------------------
Check your Progress 1
----------------------
State True or False.
----------------------
1. In a nested flow the outer loop is executed within an inner loop.
----------------------
2. The number of repetitions of inner loop can be different for every
execution of outer loop. ----------------------
3. The values of inner loop variables are reset every time the outer loop ----------------------
repeats.
----------------------

----------------------

----------------------

Advanced Constructs using Algorithms 99


Notes
Activity 1
----------------------

---------------------- Write an algorithm to find the sum of entered number.

----------------------

---------------------- 6.3 PARALLEL EXECUTION


----------------------
We have studied various ways of arranging the instructions to decide the
---------------------- order of execution. We have learnt that we can jump from one point to other or
repeat the same set of instructions depending on the need. In all these cases only
---------------------- one instruction is executed at any point of time. In complex and performance-
intensive cases, we may need part of programs to execute simultaneously.
----------------------
Whenever multiple sets of instructions have to execute simultaneously,
---------------------- we can use parallel execution. The implementation of parallel execution is
dependent on underlying operating system. In this section, we will focus on
----------------------
cases where the algorithm demands parallel execution of the instruction. We will
---------------------- not study about how it is actually executed in memory, but focus on logically
which steps should follow which.
----------------------
Parallel execution is depicted using the diagram below.
----------------------

----------------------

----------------------

----------------------

----------------------

---------------------- Fig. 6.2: Parallel Execution


In the above parallel execution, Step 3 is followed by Step 4, which is
----------------------
followed by Step 5. In the parallel, Step 7 is executed, which is followed by
---------------------- Step 8.
We need to use parallel execution whenever two sets of instructions can
----------------------
be handled independently and parallel. For example, an algorithm to optimise
---------------------- the memory utilisation may need steps to be executed in parallel.

---------------------- For optimising the memory utilisation, multiple steps may need to be
performed in parallel like checking the current memory use, freeing the memory
---------------------- occupied by unused objects, prioritising the current applications, moving the
objects occupying large amount of memory out of memory, etc. Let us try to
---------------------- write a simple algorithm for memory optimisation.
----------------------

----------------------

100 Algorithms and Programming Concepts


Example 3: Algorithm to optimise the memory utilisation. Notes
Step 1Start
----------------------
<To check the current <To free the unused <To move the objects occupy-
memory use> memory> ing large memory> ----------------------
Step 2: List the pro- Step 6: Identify the objects Step 8: Identify the objects
grams which are cur- which have no active refer- which occupy more than n
----------------------
rently running ence Mbs if memory ----------------------
Step 3: For each pro- Step 7: Free the memory Step 9: For each such object
gram go to Step 4 occupied by each such go to Step 10 ----------------------
object
Step 4: Identify the Step 10: Write the object to ----------------------
memory used disk, update the reference in
the memory ----------------------
Step 5: Add the individ- Step 11: Free the memory oc- - - - - - - - - - - - - - - - - - - - - - -
ual memory to find the cupied by the object
total memory use ----------------------
Step 12: Stop
----------------------
In this algorithm, multiple steps are executed in parallel. The order of
execution of steps in different sets cannot be predicted. So there the sequence ----------------------
of steps cannot be defined. However, the algorithm will ensure that all the steps ----------------------
are executed.
A sample execution could be: ----------------------

Step 1: Start ----------------------


Step 2 ----------------------
Step 6
Step 3 ----------------------
Step 4 ----------------------
Step 5
----------------------
Step 8
Step 9 ----------------------
Step 7 ----------------------
Step 7
Step 4 ----------------------
Step 4 ----------------------
Step 5
----------------------
Step 9
Step 10 ----------------------
Step 4 ----------------------
Step 10
Step 11 ----------------------

----------------------

Advanced Constructs using Algorithms 101


Notes Step 12: Stop
Please note that there are certain steps which repeat, such as Step 4, Step
----------------------
7, Step 10 because they will be done for each object/program, etc.
---------------------- As in this sample, the steps in parallel flow may be executed in any other
sequence.
----------------------
Only order that will be guaranteed is:
----------------------
- Step 1 will be first, Step 12 will be last
---------------------- - Step 2 will be executed before Step 3, Step 3 will be executed before Step 4,
---------------------- Step 4 will be executed before Step 5
- Step 6 will be executed before Step 7
----------------------
- Step 8 will be executed before Step 9, Step 9 will be executed before Step 10,
---------------------- Step 10 will be executed before Step 11
---------------------- Since the order of execution cannot be defined, the dependent sets of
instructions should not be placed in parallel sets of execution. The steps in
---------------------- different sets may execute in any order. Please note that Step 9 executes before
Step 7 in this example. So sequence numbering does not apply for steps in
----------------------
different sets.
---------------------- We will see another example where parallel execution is not a good idea.
---------------------- Example 4: Algorithm to withdraw money from a bank account.

---------------------- To withdraw amount X from a bank account we need to multiple things


like check the balance, deduct the money, print the balance. Let us see what
---------------------- happens when these instructions are arranged in a parallel execution.
---------------------- Step 1: Start
Step 2: Get the amount to be withdrawn
----------------------
<To check the current <To deduct the money> <To print the balance>
---------------------- balance>
---------------------- Step 3: Check the cur- Step 5: Give the user the Step 7: Print the current bal-
rent balance amount to be withdrawn ance
---------------------- Step 4: If current bal- Step 6: Current Balance =
ance is more than the Current Balance – Amount
---------------------- amount to be withdrawn to be withdrawn
then approve the with-
---------------------- drawal
---------------------- Step 8: Stop
---------------------- Let us check a sample execution of this algorithm and understand how
parallel execution can create undesired results. Assume the account has opening
---------------------- current balance as Rs 100, and amount to be withdrawn is Rs 50. Logically the
closing balance printed should be Rs 50.
----------------------

----------------------

102 Algorithms and Programming Concepts


Considering that order of actual execution of the steps in the algorithm is Notes
as below:
----------------------
Step 1
Step 2 ----------------------
Step 5
----------------------
Step 7
Step 3 ----------------------
Step 6 ----------------------
Step 4
----------------------
Step 8
In this case, the Balance at Step 5 would be Rs 100. At Step 7, Rs 100 ----------------------
would be printed and at Step 6 the balance will be updated. Thus, closing balance
----------------------
will be printed as Rs 100 (Step 7) in spite of Rs 50 being withdrawn (Step 6)!
Since the order of execution of statements in parallel flow is not predictable, ----------------------
the logic should not be split between multiple sets of instructions. In the next ----------------------
section, we will see how we can handle dependencies in a parallel execution.
Parallel executions are used in lots of performance-intensive programs ----------------------
such as operating system programming using data structures like trees, queue, ----------------------
stack, etc.
----------------------
Check your Progress 2
----------------------
Multiple Choice Single Response. ----------------------
1. The order of execution of steps in different sets in parallel execution is:
----------------------
i. Fixed
ii. Not fixed ----------------------
iii. General ----------------------
iv. Vague
2. If multiple blocks are executed in parallel, then logically dependent ----------------------
statements should be placed in: ----------------------
i. Same block
----------------------
ii. Different blocks
iii. Concurrent blocks ----------------------
iv. Display blocks ----------------------
3. The implementation of parallel execution is dependent on the
underlying: ----------------------
i. Operating system ----------------------
ii. Programmer
----------------------
iii. Kernel
iv. Shell ----------------------

Advanced Constructs using Algorithms 103


Notes 6.4 FORK AND JOIN
---------------------- In nested flow, the execution of inner loop depends on execution of
outer loop. There may be cases when we need to perform two sets of activities
---------------------- independently before proceeding further. For such cases, we can use logical
fork and join.
----------------------
‘Fork’ indicates that the execution can be split into two set of steps which
---------------------- may execute in parallel. After both the set of steps are executed, the flow is
---------------------- connected using a ‘join’ statement.
Fork means to split the execution into two. Join means to connect the two
---------------------- executions.
---------------------- Parallel execution is possible only if the operating system supports parallel
execution. Otherwise, the two sets may be executed one after the other but steps
----------------------
after the join wait for both sets to finish.
---------------------- Fork and join is depicted in the figure below:
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Fig. 6.3: Fork and Join
----------------------
In the above flow, after Step 3 there may be two separate programs. One
---------------------- set will execute Step 4, Step 5 and Step 6; the other set will execute Step 7
and Step 8. Once both the sets have finished execution then Step 9 will be
---------------------- performed.
---------------------- Steps after join do not continue unless all the steps in forked sets are
completed.
----------------------
Fork and join can be used to perform actions that can be performed in
---------------------- parallel but must be combined later to proceed. For example, to find the ratio

104 Algorithms and Programming Concepts


of literate women in a population, we must find total number of literate women Notes
and total population parallel, which could be combined later to get the literacy
ratio. ----------------------
Example 5: To find the ratio of literate women in a population. ----------------------
Step 1: Start
----------------------
Step 2: Gather the census figures
----------------------
Fork
----------------------
<Find number of literate women> <Find total population>
Step 3: Count the total number of women Step 4: Count the total number of ----------------------
people
Join ----------------------
Step 5: Women literacy ratio = Total number of literate women/Total population ----------------------
Here the order of execution could be:
----------------------
Scenario 1: When literate women count is found first
----------------------
Step 1
Step 2 ----------------------

Step 3 ----------------------
Step 4 ----------------------
Step 5
----------------------
Or
----------------------
Scenario 2: When total population is found first
Step 1 ----------------------

Step 2 ----------------------
Step 4 ----------------------
Step 3
----------------------
Step 5
----------------------
In either scenario, Step 5 is not executed unless both the sets - Step 3 and
Step 4 are completed. Thus, the dependencies can be taken care of using fork ----------------------
and join.
----------------------
Now let us correct the algorithm to withdraw money from bank account
using fork and join by adding the dependency. ----------------------
Example 6: Algorithm to withdraw money from a bank account. ----------------------
Step 1: Start
----------------------
Step 2: Get the amount to be withdrawn
----------------------
Fork
----------------------

Advanced Constructs using Algorithms 105


Notes <To check the current balance> <To deduct the money>

---------------------- Step 3: Check the current balance Step 5: Give the user the amount to be
withdrawn
---------------------- Step 4: If current balance is more than Step 6: Current Balance = Current
amount to be withdrawn then approve the Balance – Amount to be withdrawn
---------------------- withdrawal
---------------------- Join

---------------------- Step 7: Print the current balance


Step 8: Stop
----------------------
Now let us consider that order of actual execution of the steps in the
---------------------- algorithm is as below:
---------------------- Step 1

---------------------- Step 2
Fork:
----------------------
Step 5
----------------------
Step 3
---------------------- Step 6
---------------------- Step 4

---------------------- Join: Step 7


Step 8
----------------------
If you notice now, Step 7 does not execute unless Step 3, 4, 5 and 6 are
---------------------- finished. Thus, it ensures that all action on which next step is dependent are
completed before proceeding.
----------------------

---------------------- Check your Progress 3


----------------------
Fill in the blanks.
---------------------- 1. Fork and ______are used in combination.
---------------------- 2. Steps after _____ do not continue unless all the steps in forked sets
are completed.
----------------------
3. _____ is used to split the execution into two sets.
----------------------

----------------------
Activity 2
----------------------

---------------------- Enlist three situations where fork and join can be used to write an algorithm.

----------------------

106 Algorithms and Programming Concepts


Summary Notes

• In a nested flow, the body of one set of instructions contains another set of ----------------------
instructions.
----------------------
• In a nested flow every time the outer loop executes the inner loop starts
from beginning. ----------------------
• There can be special type of algorithms which need to call itself ----------------------
repetitively. It is called recursive flow.
----------------------
• Whenever multiple sets of instructions have to execute simultaneously,
then we can use parallel execution. ----------------------
• The order of execution of steps in different sets in a parallel flow cannot ----------------------
be predicted.
----------------------
• Since the order of execution cannot be defined, the dependent sets of
instructions should not be placed in parallel sets of execution. ----------------------
• Fork and join can be used to perform actions that can be performed in
----------------------
parallel but must be combined later to proceed.
• Fork indicates that the execution can be split into two set of steps which ----------------------
may execute in parallel. After both the set of steps are executed, the flow ----------------------
is connected using a join statement.
• Steps after join do not continue unless all the steps in forked sets are ----------------------
completed. ----------------------

Keywords ----------------------

----------------------
• Nested: Contained within another.
• Recursive: One which repeats itself. ----------------------
• Fork: Split the process. ----------------------

----------------------
Self-Assessment Questions
----------------------
1. Write a short note on parallel flow in an algorithm.
----------------------
2. Describe the following with examples:
i. Recursive flow ----------------------

ii. Nested flow ----------------------


3. Write a simple algorithm to generate two-dimensional array. ----------------------
4. Write a simple algorithm to sort the array of five numbers.
----------------------
5. Write a simple algorithm to find the probability of an occurrence using
fork-join. ----------------------

----------------------

Advanced Constructs using Algorithms 107


Notes Answers to Check your Progress
---------------------- Check your Progress 1

---------------------- State True or False.


1. False
----------------------
2. True
----------------------
3. True
---------------------- Check your Progress 2
---------------------- Multiple Choice Single Response.

---------------------- 1. The order of execution of steps in different sets in parallel execution is:
ii. Not fixed
----------------------
2. If multiple blocks are executed in parallel, then logically dependent
---------------------- statements should be placed in:
---------------------- i. Same block
3. The implementation of parallel execution is dependent on the underlying:
----------------------
i. Operating system
----------------------
Check your Progress 3
----------------------
Fill in the blanks.
---------------------- 1. Fork and join are used in combination.
---------------------- 2. Steps after join do not continue unless all the steps in forked sets are
completed.
----------------------
3. Fork is used to split the execution into two sets.
----------------------

---------------------- Suggested Reading

---------------------- 1. Cormen, Thomas H. Charles E. Leiserson, Ronald L. Rivest and Clifford


Stein. Introduction to Algorithms. The MIT Press.
----------------------
2. Heineman, George T., Gary Pollice and Stanley Selkow. Algorithms in a
---------------------- Nutshell. O’Reilly Media.

---------------------- 3. JaJa, Joseph. Introduction to Parallel Algorithms. Addison Wesley.

----------------------

----------------------

----------------------

----------------------

----------------------

108 Algorithms and Programming Concepts


Searching Algorithms
UNIT

7
Structure:

7.1 Introduction
7.2 Searching Algorithms
7.3 Linear Search
7.3.1 Complexity of Linear Search
7.4 Binary Search
7.4.1 Complexity of Binary Search
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Searching Algorithms 109


Notes
Objectives
----------------------
After going through this unit, you will be able to:
----------------------
l Explain the concept of searching
----------------------
l Discuss the types of searching techniques
---------------------- l Discuss the complexity of types of searching algorithms
----------------------

---------------------- 7.1 INTRODUCTION


---------------------- Searching is the algorithmic process of finding a particular item in a
---------------------- collection of items. A search typically answers either True or False as to whether
the item is present. On occasions, it may be modified to return where the item
---------------------- is found.
---------------------- Computer systems are often used to store large amounts of data from
which individual records must be retrieved according to some search criterion.
---------------------- Thus the efficient storage of data to facilitate fast searching is an important
issue. In this section, we shall investigate the performance of some searching
---------------------- algorithms and discuss the complexity of the same.
---------------------- In computer science, a search data structure is any data structure that
allows the efficient retrieval of specific items from a  set of items, such as a
----------------------
specific record from a database.
---------------------- The simplest, most general, and least efficient search structure is merely
an unordered sequential list of all the items. Locating the desired item in such
----------------------
a list, by the linear search method, inevitably requires a number of operations
---------------------- proportional to the number n of items, in the worst case as well as in the average
case. Useful search data structures allow faster retrieval; however, they are
---------------------- limited to queries of some specific kind. Moreover, since the cost of building
such structures is at least proportional to n, they only pay off if several queries
----------------------
are to be performed on the same database (or on a database that changes little
---------------------- between queries).
Static search structures are designed for answering many queries on a fixed
----------------------
database; dynamic structures also allow insertion, deletion, or modification of
---------------------- items between successive queries. In the dynamic case, one must also consider
the cost of fixing the search structure to account for the changes in the database.
----------------------

---------------------- 7.2 SEARCHING ALGORITHMS


---------------------- A search algorithm is an algorithm for finding an item with specified properties
among a collection of items. The items may be stored individually as records in
---------------------- a database or may be elements of a search space defined by a mathematical
formula or procedure, such as the roots of an equation with integer variables or
----------------------

110 Algorithms and Programming Concepts


a combination of the two, such as the Hamiltonian circuits of a graph. Notes
The two common methods of searching are linear and binary searching.
----------------------
These two searching algorithms are discussed in the subsequent topics.
----------------------
Check your Progress 1
----------------------
Fill in the blanks. ----------------------
1. ______________ is the algorithmic process of finding a particular ----------------------
item.
2. A _____________ is an algorithm for finding an item with specified ----------------------
properties. ----------------------

----------------------
7.3 LINEAR SEARCH
----------------------
This is the most natural searching method. When data items are stored
in a collection such as a list, we say that they have a linear or sequential ----------------------
relationship. Each data item is stored in a position relative to the others. These
----------------------
relative positions are the index values of the individual items. Since these index
values are ordered, it is possible for us to visit them in sequence. This process ----------------------
gives rise to our first searching technique, the sequential search.
----------------------
Figure 7.1 shows how this search works. Starting at the first item in the
list, we simply move from item to item, following the underlying sequential ----------------------
ordering until we either find what we are looking for or run out of items. If we
run out of items, we have discovered that the item we were searching for was ----------------------
not present.
----------------------

----------------------

----------------------

----------------------
Fig. 7.1: Sequential Search of a List of Integers
----------------------
The most intuitive way to search for a given ITEM in LIST is to compare
ITEM with each element of LIST one by one. ----------------------
The algorithm for a sequential search procedure is presented below. ----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

Searching Algorithms 111


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Forward iteration
----------------------
This pseudo code describes a typical variant of linear search, where the
---------------------- result of the search is supposed to be either the location of the list item where
the desired value was found or an invalid location to indicate that the desired
----------------------
element does not occur in the list.
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

---------------------- Applications

---------------------- Linear search is usually very simple to implement and is practical when
the list has only a few elements or when performing a single search in an
---------------------- unordered list.

---------------------- When many values have to be searched in the same list, it often pays to pre-
process the list in order to use a faster method. For example, one may sort the
---------------------- list and use binary search or build any efficient search data structure from it.
Should the content of the list change frequently, repeated re-organization may
---------------------- be more trouble than it is worth.
---------------------- As a result, even though in theory other search algorithms may be faster
than linear search (for instance binary search), in practice, even on medium
----------------------
sized arrays (around 100 items or less), it might be infeasible to use anything
---------------------- else. On larger arrays, it only makes sense to use other, faster search methods
if the data is large enough, because the initial time to prepare (sort) the data is
---------------------- comparable to many linear searches.

112 Algorithms and Programming Concepts


7.3.1 Complexity of Linear Search Notes
The complexity of an algorithm is the amount of a resource, such as
----------------------
time, that the algorithm requires. It is a measure of how ‘good’ the algorithm
is at solving the problem. The complexity of a problem is defined as the best ----------------------
algorithm that solves a problem.
----------------------
For searching, it makes sense to count the number of comparisons
performed. Each comparison may or may not discover the item we are looking ----------------------
for. In addition, we make another assumption here. The list of items is not
ordered in any way. The items have been placed randomly into the list. In ----------------------
other words, the probability that the item we are looking for is in any particular
----------------------
position is exactly the same for each position of the list.
If the item is not in the list, the only way to know it is to compare it ----------------------
against every item present. If there are n items, then the sequential search
----------------------
requires n comparisons to discover that the item is not there. In the case where
the item is in the list, the analysis is not so straightforward. There are actually ----------------------
three different scenarios that can occur. In the best case we will find the item
in the first place we look, at the beginning of the list. We will need only one ----------------------
comparison. In the worst case, we will not discover the item until the very last
----------------------
comparison, the nth comparison.
What about the average case? On average, we will find the item about ----------------------
halfway into the list; that is, we will compare against n2 items. Recall, however, ----------------------
that as n gets large, the coefficients, no matter what they are, become insignificant
in our approximation, so the complexity of the sequential search, is O(n). The ----------------------
given below tables 1 and 2 shows the case comparisons in sequential search of
an unordered and ordered list. ----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

Searching Algorithms 113


Notes
Check your Progress 2
----------------------

---------------------- State True or False.


1. In linear search, each data item is stored in a position relative to the
----------------------
others.
---------------------- 2. Linear search can also be described as a non-recursive algorithm.
---------------------- 3. Linear search is very complex to implement.

---------------------- 4. The complexity of an algorithm is the amount of a resource, such as


time, that the algorithm requires.
---------------------- 5. Linear searches require the collection to be sorted.
----------------------

---------------------- Activity 1
----------------------
1. Create an array to store the following 10 elements in it: 23, 52, 9, 34,
---------------------- 77, 18, 10, 42, 55, 29
2. Write the pseudocode to find the position of 10 and display it as “The
----------------------
position of element 10 is _____” (Hint: Use Sequential search method.)
---------------------- 3. Using sequential search method, write a program to create an array
---------------------- to store any 5 numbers and display the position of all those numbers,
which are divisible by 5.
---------------------- 4. Write a pseudocode to store any 5 elements in an array and display the
---------------------- square of position of entered number.
5. Write a pseudocode to create an array to store any 5 elements and
---------------------- display the elements, which are present at even positions.
---------------------- 6. Suppose you are doing a sequential search of the list [15, 18, 2, 19,
18, 0, 8, 14, 19, 14]. How many comparisons would you need to do in
----------------------
order to find the key 18?
---------------------- 7. Suppose you are doing a sequential search of the ordered list [3, 5, 6,
8, 11, 12, 14, 15, 17, 18]. How many comparisons would you need to
----------------------
do in order to find the key 13?
----------------------

----------------------

---------------------- 7.4 BINARY SEARCH


---------------------- All of the sequential search algorithms have the same problem; they walk over
the entire list. Some of our improvements work to minimize the cost of traversing
---------------------- the whole data set, but those improvements only cover up what is really a problem
with the algorithm. By thinking of the data in a different way, we can make speed
----------------------

114 Algorithms and Programming Concepts


improvements that are much better than anything sequential search can guarantee.  Notes
Consider a list in ascending sorted order. It would work to search from
the beginning until an item is found or the end is reached, but it makes more ----------------------
sense to remove as much of the working data set as possible so that the item is
found more quickly. If we started at the middle of the list we could determine ----------------------
which half the item is in (because the list is sorted). This effectively divides ----------------------
the working range in half with a single test. By repeating the procedure,
the result is a highly efficient search algorithm called binary search.  ----------------------
The Binary search or half-interval search algorithm finds the position of the
specified input value within an array, which is sorted by key value. In each step, ----------------------
the algorithm compares the search key value with the key value of the middle ----------------------
element of the array. If the key value is matched, then a matching element has
been found and its index or position is returned. ----------------------
Otherwise, if the search key is less than the middle element’s key, then the ----------------------
algorithm repeats its action on the sub-array to the left of the middle element or,
if the search key is greater, on the sub-array to the right. If the remaining array ----------------------
to be searched is empty, then the key cannot be found in the array and a message
----------------------
“not found” is displayed.
A binary search halves the number of items to check with each iteration, ----------------------
so locating an item (or determining its absence) takes logarithmic time. A binary
----------------------
search is a dichotomic divide and conquer search algorithm.
Overview ----------------------
Searching a sorted collection is a common task. A dictionary is a sorted list ----------------------
of word definitions. Given a word, one can find its definition. A telephone book
is a sorted list of people’s names, addresses and telephone numbers. Knowing ----------------------
someone’s name allows one to quickly find their telephone number and address. ----------------------
If the list to be searched contains more than a few items (a dozen, say),
a binary search will require far fewer comparisons than a linear search, but it ----------------------
imposes the requirement that the list be sorted. ----------------------
Suppose LIST is an array, which is sorted in increasing numerical order.
Then there is an extremely efficient searching algorithm, called binary search, ----------------------
which can be used to find the location of the required element. ----------------------
Algorithm
----------------------
The binary search algorithm applied to our array LIST works as follows.
During each stage of our algorithm, our search for ITEM is reduced to a segment ----------------------
of elements of LIST:
----------------------
LIST [BEG], LIST [BEG + 1], LIST [BEG + 2], ...... LIST [END].
----------------------
Note that the variable BEG and END denote the beginning and end
locations of the segment respectively. The algorithm compares ITEM with the ----------------------
middle element LIST [MID] of the segment, where MID is obtained by
----------------------

----------------------

Searching Algorithms 115


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
Initially, we begin with the entire array LIST; i.e. we begin with BEG =
---------------------- 1 and END = n, If ITEM is not in LIST, then eventually we obtain END< BEG

---------------------- This condition signals that the search is unsuccessful and in this case, we
assign LOC: =
----------------------
NULL. Here NULL is a value that lies outside the set of indices of LIST.
---------------------- We now formally state the binary search algorithm.
Binary Search Function
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

---------------------- Limitations of the Binary Search Algorithm


---------------------- The algorithm requires two conditions:
(1) The list must be sorted.
----------------------
(2) One must have direct access to the middle element in any sublist.
----------------------

116 Algorithms and Programming Concepts


7.4.1 Complexity of Binary Search Notes
The complexity of binary search in worst and average cases is O(log2n)
----------------------
and it is O(1) in  best case.
----------------------
Check your Progress 3
----------------------
Fill in the blanks. ----------------------
1. A binary search or half-interval search algorithm  finds the ----------------------
______________ of a specified input value.
2. Searching a _______________ collection is a common task. ----------------------

3. The complexity of binary search in worst and average cases is ----------------------


______________.
----------------------

----------------------
Activity 2
----------------------
1. Store the following elements in an array: 10, 25, 36, 48, 62, 100 ----------------------
Using Binary search method, write the pseudocode to find the position ----------------------
of 62.
2. Store the following elements in an array: 87, 52, 41, 26, 19, 12, 8, 5, ----------------------
3, 1 ----------------------
3. Using the Binary search method, write the pseudocode to find the
position of 52. ----------------------

----------------------
Summary ----------------------

• Searching is a process of finding the position of an element in a list. ----------------------


• Linear search is the process of traversing the entire list from beginning to ----------------------
end
----------------------
• Binary search method is the process of searching for the specified item in
a sorted list. ----------------------

----------------------
Keywords
----------------------
• Searching: Finding particular element
----------------------
• Linear Search: Sequential searching
• Binary Search: Searching in sorted array ----------------------

----------------------

----------------------

Searching Algorithms 117


Notes
Self-Assessment Questions
----------------------
1. Explain searching.
---------------------- 2. Discuss the complexities of linear and binary search algorithms with
examples.
----------------------
3. Can binary search be performed on non-sorted list, illustrate your answer.
----------------------

---------------------- Answers to Check your Progress


---------------------- Check your Progress 1

---------------------- Fill in the blanks.


1. Searching is the algorithmic process of finding a particular item.
----------------------
2. A search algorithm  is an  algorithm  for finding an item with specified
---------------------- properties.
---------------------- Check your Progress 2
State True or False.
----------------------
1. True
----------------------
2. False
---------------------- 3. False
---------------------- 4. True
---------------------- 5. False
Check your Progress 3
----------------------
Fill in the blanks.
----------------------
1. A binary search or half-interval search algorithm finds the position of a
---------------------- specified input value.
---------------------- 2. Searching a sorted collection is a common task.
3. The complexity of binary search in worst and average cases is O(log2n).
----------------------

---------------------- Suggested Reading


---------------------- 1. Anany Levitin. Introduction to the Design and Analysis of Algorithms.
---------------------- 2. Augenstein Moshe J., Langsam Yedidyah, Tenenbaum Aaron M. Data
Structures Using C And C++. Prentice Hall.
----------------------
3. K. Raghava Rao. Introduction to Design Analysis of Algorithms.
----------------------
4. Karumanchi, Narsimha. Data Structures and Algorithms Made Easy.
---------------------- 5. Schaum Series. Data Structures.
----------------------

118 Algorithms and Programming Concepts


Sorting Algorithms
UNIT

8
Structure:

8.1 Introduction
8.2 Types of Sorting Algorithms
8.3 Selection Sort
8.4 Bubble Sort
8.5 Merge Sort
8.6 Quick Sort
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Sorting Algorithms 119


Notes
Objectives
----------------------
After going through this unit, you will be able to:
----------------------
l Explain the concept of sorting and identify its need
----------------------
l Arrange an unordered set of comparable items in some order
---------------------- l Discuss the different sorting algorithms
---------------------- l Analyse the usage of these sorting algorithms in different scenarios

----------------------
8.1 INTRODUCTION
----------------------
Sorting is the technique of arranging the elements/records in a
----------------------
particular order or sequence. The arrangement is most often made as ascending
---------------------- or descending if there is numerical data and alphabetical if there is character
data. In an ascending order, the elements are arranged in increasing order. In
---------------------- descending, the elements are arranged in decreasing order. The commonly
used order is numerical or alphabetical. The process of sorting holds a major
----------------------
importance in computer science as it helps in speeding up the process of
---------------------- searching an element or a record.
Sorting helps in faster and efficient search in a database. The reverse of
----------------------
sorting is shuffling which means arranging the elements in random or unordered
---------------------- way.
The advantage of sorting is that it allows to arrange the data in a meaningful
----------------------
order, but it also is a time-consuming process.
---------------------- Let A be a list of N elements A1, A2, A3......, AN in memory. Sorting of A
---------------------- means arranging the elements of A numerically or alphabetically so that they
are either in ascending or descending order
---------------------- i.e.,
---------------------- A1< = A2 <= A3<= ….... <= AN
---------------------- or
A1 >= A2 >= A3 >= …..... >= AN
----------------------

---------------------- 8.2 TYPES OF SORTING ALGORITHMS


---------------------- To perform sorting, the computer needs to follow an algorithm, i.e., a
sequence of predefined steps. A sorting algorithm is used to arrange a group
----------------------
of unordered elements in a particular order. Different sorting algorithms have
---------------------- different efficiency and performance in terms of time taken and the space
occupied.
----------------------
There are many sorting algorithms and selection of a particular algorithm
---------------------- depends on the problem to be solved. They are selected on the basis of complexity

120 Algorithms and Programming Concepts


involved. Sorting could be comparison or non-comparison based. It could also Notes
be internal sorting or external sorting.
----------------------
In internal sorting, the data to be sorted is held in the primary memory.
While in external sorting, the data to be sorted is held in the primary memory, ----------------------
while the temporary data that helps in the sorting process is held in secondary
memory. The sorting algorithm to be used is dependent mainly on the problem ----------------------
to be solved.
----------------------
But there are other factors as well that should be kept in mind while
selecting an algorithm, such as: ----------------------
●● Type of data to be sorted ----------------------
●● Performance expected
----------------------
●● Length and complexity of code
----------------------
●● Memory requirements
Type of the data to be sorted implies whether the sorting is to be applied on ----------------------
character, words, numbers, etc. Different sorting algorithms behave differently
on the type of the data to be sorted. ----------------------

Then in different scenarios, a different type of performance is of priority. ----------------------


Some problems expect that the algorithm should run in minimum amount
----------------------
of time, while some expect the memory usage should be less. So the type of
algorithm selected also depends on the performance criterion of concern. ----------------------
Although some algorithms give better performance, the code at times
----------------------
is more complex. Therefore, the length and complexity of code varies from
algorithm to algorithm. ----------------------
There are commonly two types of sorting algorithms: ----------------------
●● Internal sorting
----------------------
●● External sorting
Internal sorting is applied in situations where the data elements to be ----------------------
sorted are small and can fit into the processor’s main memory and no extra
----------------------
space is required. Examples of internal sorting are insertion, sort, bubble sort
and selection sort. ----------------------
External sorting is applied in situations where the data to be sorted is large ----------------------
enough to fit into the processor’s memory. The source data and the final result
are stored in hard disks and tapes, i.e., it requires auxiliary storage. The data ----------------------
is brought into memory for processing a portion at a time and can be accessed
sequentially. Merge sort is one example of sorting technique that is used in case ----------------------
of external sorting. Sorting with tapes is very similar to sorting with disks, the ----------------------
only difference being some amount of time is spent in sequentially searching
the data. ----------------------
When data resides in internal memory, then the data access time is less ----------------------
than the computation time and there is a need to reduce the number of CPU
operations. ----------------------

Sorting Algorithms 121


Notes While when the data resides in the external memory, data access time is
always greater than the computational time and there is a need to reduce the
---------------------- number of disk accesses.
----------------------
Check your Progress 1
----------------------
Multiple Choice Single Response.
----------------------
1. The operation of processing each element in a list is called:
---------------------- i. Sorting
---------------------- ii. Traversing
iii. Merging
----------------------
iv. Inserting
---------------------- 2. Sorting is useful for:
---------------------- i. Report generation
ii. Minimising the storage needed
---------------------- iii. Making searching easier and efficient
---------------------- iv. Responding to queries easily
3. Choose the correct statement.
----------------------
i. Internal sorting is used if the number of items to be sorted is very
---------------------- large.
ii. External sorting is used if the number of items to be sorted in
----------------------
very large.
---------------------- iii. External sorting needs auxiliary storage.
iv. Internal sorting needs auxiliary storage.
----------------------
Fill in the blanks.
---------------------- 1. Finding the location of the element with a given value is called
____________.
----------------------
2. The elements can be arranged in___________ and ____________.
---------------------- State True or False.
---------------------- 1. When data resides in primary memory, data access time is more.

----------------------

---------------------- Activity 1
----------------------
1. For the given alphabets in the word “COMPUTER”, arrange the
---------------------- elements in ascending and descending order.
2. Suppose S is the following list of 14 alphabetic characters:
----------------------
“DATASTRUCTURE”.
----------------------
Suppose the characters in S are to be sorted alphabetically. Sort the
---------------------- elements in ascending and descending order.

122 Algorithms and Programming Concepts


8.3 SELECTION SORT Notes
Selection sort works recursively to select an element and place it in ----------------------
the correct position in the sequence. For example, to arrange the elements in
ascending order, the smallest element is selected from the list and placed at the ----------------------
first position. Then the next smallest element is selected from the remaining list
----------------------
and placed at the second position, and so on till the list is sorted.
Location 1 2 3 4 5 6 7 8 9 10 ----------------------
Data 41 32 11 45 27 63 34 12 56 48 ----------------------
1st Iteration loc=3 11 32 41 45 27 63 34 12 56 48
2nd Iteration loc=8 11 12 41 45 27 63 34 32 56 48 ----------------------
3rd Iteration loc=5 11 12 27 45 41 63 34 32 56 48
4th Iteration loc=8 11 12 27 32 41 63 34 45 56 48 ----------------------
5th Iteration loc=7 11 12 27 32 34 63 41 45 56 48
6th Iteration loc=7 41 63 ----------------------
11 12 27 32 34 45 56 48
7th Iteration loc=8 11 12 27 32 34 41 45 63 56 48 ----------------------
8th Iteration loc=10 11 12 27 32 34 41 45 48 56 63
9th Iteration loc=9 11 12 27 32 34 41 45 48 56 63 ----------------------
As seen in the above example, in the first iteration, the smallest element ----------------------
in the entire list is 11. This element is swapped with the element at the first
position since we are arranging the list in an ascending order. Therefore, the ----------------------
first iteration ensures that the first element is at the correct sequence in the list.
----------------------
In the second iteration, the smallest element is found in the remaining list
leaving the first element, since it is at its proper position. The smallest element ----------------------
in the remaining list is 12 which is swapped with the element in the second
position, hence placing 12 in the correct sequence in the list. ----------------------

This process is continued till the last element in the list. The elements ----------------------
underlined shows the elements which are swapped. The number of iterations is
directly proportional to the number of elements in the list. In the above example, ----------------------
the number of elements are 10 and hence require 9 iterations to get a sorted list. ----------------------
This method also does not require additional storage.
----------------------
If the size of the array is n, then the number of comparisons to find the
largest element in each sub-array is n+(n-1)+(n-2)+....+2+1 comparisons, which ----------------------
is equal to n(n-1)/2 comparisons.
----------------------
At each iteration, a swapping is performed. So if the number of elements
is n, then in all (n-1), comparisons are required. ----------------------
Algorithm ----------------------
1. for I=1 to N-1
----------------------
2. min=A [I]
----------------------
3. for K=I+1 to N
4. if (min>A [I]) ----------------------
5. min=A [K], Loc=K ----------------------

Sorting Algorithms 123


Notes 6. Swap (A [Loc],A[I])
7. Exit
----------------------
Advantages
----------------------
1. It is an in-place sorting algorithm and does not require extra storage.
---------------------- Hence, it is an example of internal sorting.

---------------------- Disadvantages
1. It works better in smaller lists.
----------------------
2. The performance is also affected by the initial ordering of the elements.
----------------------
3. It gives poor efficiency when applied on a large list.
---------------------- 4. There is an extra overhead involved in finding the largest element in each
iteration.
----------------------

---------------------- Check your Progress 2


----------------------
Fill in the blanks.
----------------------
1. In a selection sort of n elements, ________ number of times is the
---------------------- swap function called in the complete execution of the algorithm.
2. In the first iteration, ___________ element in the list is put at its
----------------------
appropriate position.
---------------------- State True or False.
---------------------- 1. Selection sort is an example of internal sorting.

---------------------- Multiple Choice Single Response.


1. The way a card game player arranges his cards as he picks them up
---------------------- one by one is an example of:
---------------------- i. Bubble sort
---------------------- ii. Selection sort
iii. Insertion sort
----------------------
iv. Merge sort
----------------------

----------------------
Activity 2
----------------------

---------------------- Here is an array of ten integers:


5389170264
----------------------
Draw this array after the FIRST iteration of the selection sort (sorting
---------------------- from smallest to largest) is done.
----------------------

124 Algorithms and Programming Concepts


8.4 BUBBLE SORT Notes
Bubble sort works repeatedly by swapping elements in the adjacent places ----------------------
that are not in order, till the entire list is in a particular order.
----------------------
1st Iteration
----------------------
Data 41 32 11 45 27 63 34 12 56 48
32 41 11 45 27 63 34 12 56 48 ----------------------
32 11 41 45 27 63 34 12 56 48
32 11 41 45 27 63 34 12 56 48 ----------------------
32 11 41 45 27 63 34 12 56 48
32 11 41 45 27 63 34 12 56 48 ----------------------
32 11 41 45 27 34 63 12 56 48
----------------------
32 11 41 45 27 34 12 63 56 48
32 11 41 45 27 34 12 56 63 48 ----------------------
32 11 41 45 27 34 12 56 48 63
2nd Iteration ----------------------

Data 32 11 41 45 27 34 12 56 48 63 ----------------------
11 32 41 45 27 34 12 56 48 63
11 32 41 45 27 34 12 56 48 63 ----------------------
11 32 41 45 27 34 12 56 48 63 ----------------------
11 32 41 27 45 34 12 56 48 63
11 32 41 27 34 45 12 56 48 63 ----------------------
11 32 41 27 34 12 45 56 48 63
11 32 41 27 34 12 45 56 48 63 ----------------------
11 32 41 27 34 12 45 48 56 63
----------------------
3rd Iteration
----------------------
Data 11 32 41 27 34 12 45 48 56 63
11 32 41 45 27 34 12 56 48 63 ----------------------
11 32 41 45 27 34 12 56 48 63
11 32 41 45 27 34 12 56 48 63 ----------------------
11 32 41 27 45 34 12 56 48 63
11 32 41 27 34 45 12 56 48 63 ----------------------
11 32 41 27 34 12 45 56 48 63 ----------------------
11 32 41 27 34 12 45 56 48 63
4th Iteration ----------------------

Data 11 32 41 27 34 12 45 56 48 63 ----------------------
11 32 41 45 27 34 12 56 48 63
11 32 41 45 27 34 12 56 48 63 ----------------------
11 32 41 45 27 34 12 56 48 63 ----------------------
11 32 41 27 45 34 12 56 48 63
11 32 41 27 34 45 12 56 48 63 ----------------------
11 32 41 27 34 12 45 56 48 63
----------------------
As can be seen from the above example, each iteration requires a set of
comparisons to be made. After the first iteration, the number that is the largest ----------------------

Sorting Algorithms 125


Notes reaches its correct position in the list and hence need not be compared in the
next iteration. So the number of comparison keeps on decreasing with each
---------------------- iteration.
---------------------- The total number of iterations is one less than the total number of elements.
So for the above example, the total number of iterations is 9, which is the same
---------------------- as selection sort. But the number of comparisons made in bubble sort is more as
compared to selection sort.
----------------------
Number of comparison=9+8+7+6+5+4+3+2+1=45
----------------------
Number of iterations=10 -1=9
---------------------- Algorithm
---------------------- 1. for I=1 to N-1 (for pass)
---------------------- 2. for k=1 to N-I (for comparison)
3. if A[K]>A[K+1]
----------------------
4. swap [A(k) , A(k+1)]
----------------------
5. End if
---------------------- 6. End For
---------------------- 7. End For
---------------------- Advantages
1. It is easy to implement since it is straightforward and simple.
----------------------
2. It is also an in-place sorting algorithm and does not require additional
---------------------- storage to do sorting. Hence, it is an example of internal sorting.
---------------------- Disadvantages

---------------------- 1. It is not suitable for a large list.


2. It requires n squared processing for n elements in a list.
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

126 Algorithms and Programming Concepts


Notes
Check your Progress 3
----------------------
Multiple Choice Single Response. ----------------------
1. The number of swapping needed to sort the numbers 8, 22,7, 9, 31,19,
----------------------
5,13 in ascending order using bubble sort is:
i. 11 ----------------------
ii. 12 ----------------------
iii. 13 ----------------------
iv. 14
----------------------
2. The number of interchanges required to sort 5, 1, 6, 2, 4 in ascending
order using bubble sort is ----------------------
i. 6 ----------------------
ii. 5
----------------------
iii. 7
----------------------
iv. 8
----------------------

Activity 3 ----------------------

----------------------
Here is an array of ten integers:
----------------------
5389170264
----------------------
Draw this array after the FIRST iteration of bubble sort (sorting from
smallest to largest). ----------------------

----------------------
8.5 MERGE SORT
----------------------
Merge sort is based on the concept of divide and conquer. In this approach,
----------------------
a larger problem set is divided into smaller sub-lists and the solution is applied
on the smaller sub-lists. Initially, the entire list is divided into two smaller sub- ----------------------
lists, which in turn is divided into two smaller sub-lists, and so on till a sub-list
is contains two numbers. The two numbers are sorted. The smaller sub-lists are ----------------------
combined together to get a sub-list of four numbers. This process is repeated
----------------------
till the entire list is combined together. Merge sort is an example of external
sorting. The entire process of dividing the list and combining the smaller sub- ----------------------
lists is a recursive process. It is a recursive sorting procedure.
----------------------
To sort an array of n elements, merge sort process goes through the
following steps: ----------------------

----------------------

Sorting Algorithms 127


Notes 1. If size of the array n is less than 2, then the array is assumed to be already
sorted.
----------------------
2. If the size of the array, n is greater than 1, then the following three steps
---------------------- are performed:
n Sort the left half of the array using Merge Sort.
----------------------
n Sort the right half of the array using Merge Sort.
----------------------
n Merge the sorted left and right halves to get a final sorted list.
---------------------- Algorithm and Example
---------------------- MergeSort(array A, int p, int r)
---------------------- {
if (p < r)
----------------------
{ // we have at least 2 items
----------------------
q = (p + r)/2
---------------------- MergeSort(A, p, q) // sort A[p..q]
---------------------- MergeSort(A, q+1, r) // sort A[q+1..r]
---------------------- Merge(A, p, q, r) // merge everything together
}
----------------------
}
----------------------

---------------------- Merge(array A, int p, int q, int r) // merges A[p..q] with A[q+1..r]


---------------------- {
---------------------- array B[p..r]

---------------------- i = k = p // initialize pointers


j = q+1
----------------------
while (i <= q and j <= r) // while both subarrays are nonempty
----------------------
{
---------------------- if (A[i] <= A[j]) B[k++] = A[i++] // copy from left subarray
---------------------- else B[k++] = A[j++] // copy from right subarray

---------------------- }
while (i <= q) B[k++] = A[i++] // copy any leftover to B
----------------------
while (j <= r) B[k++] = A[j++]
----------------------
for i = p to r do A[i] = B[i] // copy B back to A
---------------------- }
----------------------

128 Algorithms and Programming Concepts


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

Advantages ----------------------
1. Works well for very large list. ----------------------
2. Stable algorithm. ----------------------
3. It is a fast recursive sorting process.
----------------------
4. Useful for both internal and external sorting.
----------------------
5. It requires a temporary storage that is as large as the original array to be
sorted. ----------------------
Disadvantages
----------------------
1. The process is time-consuming.
----------------------
Check your Progress 4 ----------------------

Multiple Choice Single Response. ----------------------

1. Merge sort makes two recursive calls. Which statement is true after ----------------------
these recursive calls finish, but before the merge step?
----------------------
i. The array elements form a heap.
----------------------
ii. Elements in each half of the array are sorted amongst themselves.
iii. Elements in the first half of the array are less than or equal to ----------------------
elements in the second half of the array. ----------------------
iv. None of the above.
----------------------

----------------------
Activity 4
----------------------
Here is an array of ten integers: ----------------------
5389170264
----------------------
Draw this array after two recursive calls of merge sort are completed, and
before the final merge step has occurred. ----------------------

----------------------

Sorting Algorithms 129


Notes 8.6 QUICK SORT
---------------------- Quick sort algorithm was invented by C.A.R. Hoare in 1962. The name of
the algorithm is synonymous with the fact that it is faster as compared to other
---------------------- sorting algorithms.
---------------------- Quick sort is based on the divide and conquer approach and works best
on a small list. As the approach is divide and conquer, the original list is divided
---------------------- into two smaller sub-lists. A pivot element is selected. All the elements in one
---------------------- sub-list is smaller than the pivot element and all the elements in the other sub-
list is greater than the pivot element. The process is applied recursively to the
---------------------- sub-lists generated till it reaches a sub-list of size 2. Finally, all the sub-lists are
combined to get a final sorted list.
----------------------
If the number of elements to be sorted is 0 or 1, it returns and starts
---------------------- combining the smaller sub-lists.
---------------------- It is an example of divide and conquer sorting. The performance of quick
sort is dependent on the choice of the pivot element. If the pivot element selected
---------------------- is not appropriate the complexity of the algorithm increases.
---------------------- Quick sort follows the steps as under:
1. Divide the problem into smaller sorting problems.
----------------------
2. Select a pivot element and rearrange the elements in such a way that:
----------------------
n All elements to the left of the pivot are smaller than the pivot
---------------------- element.
---------------------- n All the elements to the right of the pivot are greater than the pivot
element.
----------------------
n The pivot element has taken final place in the sorted list.
---------------------- 3. The smaller sub-lists are again combined to produce a final sorted list.
---------------------- Here is a diagrammatic representation of quick sort algorithm.

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

130 Algorithms and Programming Concepts


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

Fig. 8.1: Quick Sort Algorithm ----------------------


Advantages ----------------------
1. It is able to process large lists fast. ----------------------
2. No additional storage is required since it swaps the elements in their
current places to get a sorted arrangement. ----------------------

3. It is the fastest sorting algorithm in practice. ----------------------


Disadvantages ----------------------
1. Unbalanced partition due to the choice of the pivot element can lead to a
----------------------
very slow sort.
2. It is unstable in nature. ----------------------

----------------------

Sorting Algorithms 131


Notes Algorithm
Partition (A, p, r)
----------------------
1. x = A[r]
---------------------- 2. i = p-1
---------------------- 3. For j = p to r-1
4. Do if A[j]<=x
---------------------- 5. Then i = i+1
---------------------- 6. Exchange A[i], A[j]
7. Exchange A[i+1], A[r]
----------------------
8. Return i+1
----------------------
Check your Progress 5
----------------------

---------------------- Multiple Choice Single Response.

---------------------- 1. Suppose we are sorting an array of eight integers using quicksort, and
we have just finished the first partitioning with the array looking like
---------------------- this:
---------------------- 2 5 1 7 9 12 11 10
Which statement is correct?
----------------------
i. The pivot could be either 7 or 9.
----------------------
ii. The pivot could be 7, but it is not 9.
---------------------- iii. The pivot is not 7, but it could be 9.
---------------------- iv. Neither 7 nor 9 is the pivot.
---------------------- Fill in the blanks.
1. Quick Sort is based on the divide and _____________ strategy.
----------------------
2. Quick Sort was invented by _____________________.
----------------------
State True or False.
---------------------- 1. The sub-lists are generated till the smaller sub-lists are of size smaller
---------------------- than 2.

----------------------
Activity 5
----------------------

---------------------- Here is an array which has just been partitioned by the first step of quick sort:
---------------------- 3, 0, 2, 4, 5, 8, 7, 6, 9
Which of these elements could be the pivot? (There may be more than one
----------------------
possibility!)
----------------------

132 Algorithms and Programming Concepts


Summary Notes

●● Sorting is a very important process and has many applications in various ----------------------
fields.
----------------------
●● Sorting helps in finding a record or data easily in a set of data.
----------------------
●● he selection of sorting algorithm is dependent on various factors
T
including the type of data, etc. ----------------------
●● Merge sort is a typical example of external sorting.
----------------------

Keywords ----------------------

●● Sorting: Arranging elements in a particular order. ----------------------


●● I nternal sorting: Data sorting process that takes place entirely within ----------------------
the main memory of a computer.
●● xternal sorting: Class of sorting algorithms that can handle massive
E ----------------------
amounts of data. ----------------------
●● election sort: A sorting algorithm, specifically an in-place comparison
S
sort. ----------------------

----------------------
Self-Assessment Questions
----------------------
1. Suppose the following numbers are stored in an array A.
----------------------
32, 51, 27, 85, 66, 23, 13, 57.
----------------------
Sort the array using bubble sort.
2. Explain selection sort with the data of 10 numbers. ----------------------

----------------------
Answers to Check your Progress
----------------------
Check your Progress 1
----------------------
Multiple Choice Single Response.
1. The operation of processing each element in a list is called: ----------------------

ii. Traversing ----------------------


2. Sorting is useful for: ----------------------
iii. Making searching easier and efficient
----------------------
3. Choose the correct statement.
----------------------
ii. External sorting is used if the number of items to be sorted in very
large. ----------------------

----------------------

----------------------

Sorting Algorithms 133


Notes Fill in the blanks.
1. Finding the location of the element with a given value is called searching.
----------------------
2. The elements can be arranged in ascending order and descending order.
----------------------
State True or False.
---------------------- 1. False
---------------------- Check your Progress 2
---------------------- Fill in the blanks.

---------------------- 1. In a selection sort of n elements, (n-1) number of times is the swap


function called in the complete execution of the algorithm.
---------------------- 2. In the first iteration, largest element in the list is put at its appropriate
---------------------- position.
State True or False.
----------------------
1. True
----------------------
Multiple Choice Single Response.
---------------------- 1. The way a card game player arranges his cards as he picks them up one
---------------------- by one is an example of:
i. Selection sort
----------------------
Check your Progress 3
----------------------
Multiple Choice Single Response.
---------------------- 1. The number of swapping needed to sort the numbers 8, 22,7, 9, 31,19,
5,13 in ascending order, using bubble sort is:
----------------------
i. 11
----------------------
2. The number of interchanges required to sort 5, 1, 6, 2, 4 in ascending
---------------------- order using bubble sort is:

---------------------- ii. 5
Check your Progress 4
----------------------
Multiple Choice Single Response.
----------------------
1. Merge sort makes two recursive calls. Which statement is true after these
---------------------- recursive calls finish, but before the merge step?

---------------------- iii. Elements in the first half of the array are less than or equal to
elements in the second half of the array.
----------------------

----------------------

----------------------

----------------------

134 Algorithms and Programming Concepts


Check your Progress 5 Notes
Multiple Choice Single Response.
----------------------
1. Suppose we are sorting an array of eight integers using quicksort, and we
have just finished the first partitioning with the array looking like this: ----------------------
2 5 1 7 9 12 11 10 ----------------------
Which statement is correct? ----------------------
i. The pivot could be either 7 or 9.
----------------------
Fill in the blanks.
----------------------
1. Quick sort is based on the divide and conquer strategy.
2. Quick sort was invented by C.A.R. Hoare. ----------------------
State True or False. ----------------------
1. True ----------------------

Suggested Reading ----------------------

----------------------
1. Lipschutz. Data Structures with C. New Delhi: Tata McGraw-Hill.
2. Karumanchi, Narasimha. Data Structures and Algorithms Made Easy. ----------------------
Careermonk Publications.
----------------------
3. Tanenbaum, A. M., Yedidyah Langsam and Moshe J. Augenstein. Data
Structures using C and C++. Prentice Hall. ----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

Sorting Algorithms 135


Notes

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

136 Algorithms and Programming Concepts


Special Problems and Algorithms
UNIT

9
Structure:

9.1 Introduction
9.2 Divide and Conquer Algorithms
9.3 Dynamic Programming
9.4 Greedy Algorithms
9.5 P and NP Complete Problems
Summary
Keywords
Self-Assessment Questions
Answers to Check your Progress
Suggested Reading

Special Problems and Algorithms 137


Notes
Objectives
----------------------
After going through this unit, you will be able to:
----------------------
l Use divide and conquer algorithms
----------------------
l Explain dynamic programming
---------------------- l Analyse the greedy algorithms
----------------------

---------------------- 9.1 INTRODUCTION


---------------------- In the previous units we have studied the concept of what is an algorithm
and why there is a need to design the algorithms. We have also understood
----------------------
how to analyse the complexity of the algorithms and what are the best ways
---------------------- to write an algorithm. We have also understood different sorting and searching
algorithms. In this unit it is our objective to understand special cases and
---------------------- problems in algorithm.
----------------------
9.2 DIVIDE AND CONQUER ALGORITHMS
----------------------
Divide and conquer uses a top-down approach in its algorithms. It
---------------------- divides the question or problem into small sub-questions or sub-problems.
This approach works on the school of thought that, deriving solutions for small
----------------------
modules is easier than deriving solution to the original question or problem. Let
---------------------- us understand now the steps to be followed for ‘Divide and Conquer’ algorithms.
Steps in ‘Divide and Conquer’ Algorithm
----------------------
1. Division of the main problem into multiple sub-problems or parts that are
---------------------- the same as the original problem but in smaller sizes.
---------------------- 2. The sub-parts are solved recursively i.e. sequentially, consistently and
independently. Find answers for the sub-parts recursively i.e. successively
---------------------- and independently.
---------------------- 3. Generate a combination of solutions of the sub-parts to create a solution
to the main problem.
----------------------
This algorithm is applied in different types of problems. Let us apply this
---------------------- in “searching” problem.
---------------------- Example: Binary Search
Binary Search is a popular example of divide-and-conquer paradigm.
----------------------
Given an ordered i.e. sorted array of n elements, the concept of binary search
---------------------- searches a given element, by first finding the middle element of the array. Thus
it creates two segments of the array- lower having elements lesser then the
---------------------- middle. Upper- having elements greater than the middle.
----------------------

138 Algorithms and Programming Concepts


If the element is lesser than the middle, it searches the lower segment. The Notes
searching of the element is continued till the element is reached. Initially it is
searched irrespective of the location of the array. This continues depending on ----------------------
the outcome of the search, until the element is reached.
----------------------
Problem:
----------------------
Let A[n] be an array of non-decreasing sorted order; i.e A[i] ≤A[j]
whenever1≤ i≤ j≤ n. ----------------------
Problem: To find ‘x’ in Array A. If it is not found in A, what would be the
----------------------
appropriate position to insert x in A?
The Problem can be mathematically framed as: 1≤ I ≤ n+1andA[i-1]< x ----------------------
≤A[i]. ----------------------
Solution:
----------------------
The solution for this problem can be achieved by either sequential search
or binary search. ----------------------
●● By applying Sequential Search: It traverses or reads sequentially the ----------------------
Array A. It reads each element of A until the end of array A or finds an
element which is just greater than ‘x’. ----------------------
Sequential search for the element ‘x’ in array A with index i = 1 to n compare ----------------------
the element of Array A with ‘x.’
----------------------
if A [i] ≥x then
return index return n + 1 ----------------------
Complexity Analysis: ----------------------
This algorithm clearly takes a θ(r), where r is the index returned. The ----------------------
algorithm performs Ω(n) in the worst case and O(1) in the best case.
If all the members or elements of the array A are unique and query point ----------------------
‘x’ is at actual in the array then the loop (search condition) executes (n + 1) / 2 ----------------------
average number of times. The complexity analysis shows that the algorithm in
average and worst case uses θ(n) time. ----------------------
●● By applying Binary Search ----------------------
i) Binary Search uses an Array of sorted elements.
----------------------
ii) If the array is not sorted, sort it
----------------------
iii) Find the mid-point of the Array and divide the Array in two sub-
parts. Compare the element ’x’ with the mid-point of the Array. If ----------------------
mid-point is equal to ‘x’, than ’x’ is found.
----------------------
iv) If ‘x’ >mid-point then check for ‘x’ in the second sub-part of the
Array by finding the new mid-point of the Array. Again, compare ----------------------
’x’ with the new-point. If ‘x’ is equal to the new mid-point, ‘x’ is
found. ----------------------

----------------------

Special Problems and Algorithms 139


Notes v) If ‘x’ <mid-point then check for ‘x’ in the first sub-part of the Array
by finding the new mid-point of the Array. Again, compare ’x’ with
---------------------- the new-point. If ‘x’ is equal to the new mid-point, ‘x’ is found.
---------------------- vi) Continue the search in this way either till the element ’x’ is found or
if ‘x’ is not found, we may find the appropriate location, where ‘x’
---------------------- would be placed or ‘x’ would have been.
---------------------- Analysis:
The analysis of the Binary Search shows that it can be completed in
----------------------
logarithmic time in case of the worst case scenario, i.e., it takes T(n) =θ(log n).
---------------------- It also takes logarithmic time in the best case scenario.

---------------------- Significance of Iterative Version of Binary Search


The iterative binary search for element ‘x’ in an Array A, can be represented
---------------------- as follows:
---------------------- Array A [1 . . n],
---------------------- if x >A [n] then
return n + 1 i= 1; j = n;
----------------------
while i<j do k= (i + j)/2
----------------------
if x ≤ A [k] then j = k
---------------------- else
---------------------- i= k + 1
---------------------- return i(the index);
Analysis:
----------------------
The analysis of the Iterative version of the binary search algorithm is very
---------------------- similar to that of its recursive counterpart. In simple terms if it takes binary
---------------------- search 30 seconds to find an element in 20,000 records, it will take 30 seconds
and a little more i.e. 33 or 34 seconds to find an element in 40,000 records.
----------------------
9.3 DYNAMIC PROGRAMMING
----------------------
Dynamic programming basically follows the same approach as divide-
----------------------
and-conquer, the difference being the storage structures. It is an approach which
---------------------- uses divide-and-conquer methodology with a table. As compared to divide-and-
conquer, the approach of dynamic programming is much stronger and refined
---------------------- as a designing technique. Dynamic Programming uses different stages for
searching and optimizing the search, resulting in better decision making which
----------------------
leads to a series of step-wise decisions. The best property of this strategy is that
---------------------- for searching a solution it avoids full enumeration by trimming early partial
decision solutions that cannot possibly lead to optimal solution. This strategy
---------------------- provides the optimal solution in a polynomial approach of decision making
steps. However, in extreme cases, this strategy may conclude performing
----------------------
complete enumeration.
140 Algorithms and Programming Concepts
Dynamic programming takes benefit of the duplication in search-strategy Notes
and arranges to solve each sub-problem only once, saving the solution either
in table or in a data structure which is stored in a place, globally accessible for ----------------------
future use. The strategy of dynamic programming is to avoid calculating the
same matter twice, usually by storing the intermittent results of the sub-modules ----------------------
generally in a table. While the divide-and-conquer algorithm resolves sub- ----------------------
problems using a top-down approach dynamic programming uses a bottom-up
technique. The dynamic programming technique is very allied to divide-and- ----------------------
conquer, in the sense that it breaks down the problems into smaller sub-parts
and solves them recursively. It is found that owing to the behaviour of dynamic ----------------------
programming problems, the standard divide-and-conquer solutions are usually ----------------------
less efficient.
----------------------
Dynamic programming is powerful enough for designing algorithms related to
optimization problems. This is so for the following reasons: ----------------------
●● Finds solutions to problems with optimal value.
----------------------
●● Further, performing minimization or maximization.
----------------------
●● ynamic programming is designed for an optimization problem which is
D
solved by a permutation and combination of storing sub-problem result ----------------------
sets or solutions and conforming to the “principle of optimality.”
Steps involved in Dynamic Programming Algorithm ----------------------

The steps can be broken down into three stages as follows: ----------------------
a) Substructure: Once the problem definition is done, decompose the ----------------------
given problem into smaller and simpler sub-problems. Represent the
solution in reference to the sub-problems. The significance of Dynamic ----------------------
Programming allows many de-compositions of the solutions depending
----------------------
on the final output.
b) Table-Structure: After the decomposition and there-after, the solutions ----------------------
or the inter-mitten results of the sub-problems are stored in tables. This is
----------------------
done because the results of the sub-problems can be reused again. Saving
the results in tables saves time and optimizes the execution. We need not ----------------------
take efforts to repeatedly solve the same problem.
----------------------
c) Computation: The computation is done using a bottom-up approach.
Using a table or the stored result sets of the smaller sub-problems are ----------------------
merged. The combined or merged result is used to derive the final solution
for the complete problem. The bottom-up concept is used as follows: ----------------------

i) Begin with the smallest sub-problems. ----------------------


ii) Solve the sub-problems. ----------------------
iii) Combine the solutions of the sub-problems of increasing size.
----------------------
iv) Do this till the solution of the original problem is derived.
----------------------
Once it’s decided that dynamic programming technique is to be used,
the most important step is the formulation of the problem. In other ----------------------

Special Problems and Algorithms 141


Notes words, the most important question in designing a dynamic programming
solution to a problem is how to set up the sub-problem structure.
----------------------
Again, if it is not optimization in a problem, where should dynamic
---------------------- programming be used? The optimal structure must imply dynamic
programming. Optimal structure is a problem which needs to have
---------------------- selection of sub-program which one is the best.
---------------------- Optimal substructure varies across problem domains:
a) How many sub-problems are used in an optimal solution?
----------------------
b) How  many choices in determining which sub-problem(s) should/
---------------------- can be used?
---------------------- But, dynamic programming does not address all optimization problems.  
---------------------- Polynomial (Overlapping) Sub-problems
An important aspect of dynamic programming in reference to efficiency
----------------------
is that,it allows to solve the sub-parts equivalent to at most one polynomial.
---------------------- When the algorithm is solved recursively for the same sub-part, overlapping
occurs. But, this behaviour of the algorithm has exceptions. For example, the
---------------------- merge-sort algorithm which implements the divide-and-conquer algorithm
usually generates a brand new problem at each stage of recursion. An alternative
----------------------
approach so-called memorization also exists which works as follows:
---------------------- ●● It enforces to store the results instead of re-compute/reprocessing;
---------------------- ●● It creates the tables indexed by the sub-problem;
●● When solving a sub-problem it is essential to follow these steps:
----------------------
 After problem definition
----------------------
 Sub-problem designing
----------------------  Initially, check in the Lookup table.
----------------------  If the sub-part is solved and the solution exists in the table, use it.

----------------------  Otherwise, compute the sub-part, find the solution and then store it


in the table for future use.
---------------------- In dynamic programming, we can go one step further. We can determine
---------------------- in what order we would want to access the table, and fill it in that particular
order.
----------------------
Dynamic-Programming Solution to the 0-1 Knapsack Problem
---------------------- Problem: A thief robbing a store can carry a maximal weight of W  in his
knapsack. There are n items and ith  item weigh wi and is worth vi dollars. What
----------------------
items should the thief take?
---------------------- There are two versions of this problem:
---------------------- 1. Fractional knapsack problem: The setup is the same, but we can
modify the design, by allowing the thief can to take few parts of the total
----------------------

142 Algorithms and Programming Concepts


items. i.e. the items can be broken into smaller pieces so that the thief Notes
may decide to carry only a part of the total items. This can be represented
mathematically as: part/fraction  xi of item i, where 0 ≤ xi ≤ 1. ----------------------
This modification shows the following properties: ----------------------
a) The algorithm now displays greedy choice property.
----------------------
b) Since it shows greedy choice property, the Greedy algorithm exists.
----------------------
c) It also displays optimal substructure property.
2. Dynamic-0-1-Knapsack problem: The setup is the same, but the items ----------------------
may not be broken into smaller pieces, so thief may decide either to take ----------------------
an item or to leave it (binary choice), but may not take a fraction of an
item. ----------------------
a) Exhibit No greedy choice property. ----------------------
b) No greedy algorithm exists. ----------------------
c) Exhibit optimal substructure property. ----------------------
d) Only dynamic programming algorithm exists. ----------------------
Solution:
----------------------
Let  i be the highest-numbered item in an optimal solution S for W pounds.
Then S` = S - {i} is an optimal solution for W - wi pounds and the value to the ----------------------
solution S is Vi plus the value of the sub-problem.
----------------------
We can express this fact in the following formula: define c[i, w] to be the
solution for items  1,2, . . . , i and maximum weight w; then ----------------------
0 if i = 0 or w = 0 ----------------------
c[i,w]  = c[i-1, w] if wi ≥ 0
max [vi + c[i-1, w-wi], c[i-1, w]} if i>0 and w ≥  wi ----------------------

This says that the value of the solution to  i  items ----------------------
either a) include ith  item: Here it is vi  plus a sub-problem solution for (i  - 1)
items and the weight excluding wi or does not include ith item. Here it is a sub- ----------------------
problem›s solution for (i - 1) items and the same weight. ----------------------
That is, if the thief picks item i, having vi value, and he can choose from
items w - wi, and get c[i - 1, w - wi] additional value. On other hand, if thief ----------------------
decides not to take item i, He can choose from item 1,2, . . . , i- 1 up-to the ----------------------
weight limit w, and get c[i - 1, w] value.
----------------------
The algorithm suggests that the better of these two choices should be
made. ----------------------
The algorithm takes as input the maximum weight W, the number of items
----------------------
n, and the two sequences v = <v1, v2, . . . , vn> and w = <w1, w2, . . . , wn>. These
two sequences are stored in a two-dimensional array like A[i, j]values in the ----------------------
table. We can represent the array as follows: i.e., array A[0 . . n, 0 . . w].
----------------------

Special Problems and Algorithms 143


Notes The entries are calculated/processed in a row-major order. That is, the
first row of Array is filled in from left to right, then the second row, and so on.
---------------------- At the end of the computation, A[n, w] contains the maximum value that can be
picked into the knapsack.
----------------------
Dynamic-0-1-knapsack (v, w, n, W) we can be defined mathematically,
----------------------
FOR w = 0 TO W
---------------------- DO  A[0, w] = 0
FOR i=1 to n
----------------------
DO A[i, 0] = 0
---------------------- FOR w=1 TO W
---------------------- DO IF f wi ≤ w
THEN IF  vi + A[i-1, w-wi]
----------------------
THEN A[i, w] = vi + A[i-1, w-wi]
---------------------- ELSE A[i, w] = A[i-1, w]
---------------------- Once the set is finalized and stored in the table, we can use the set to
decide which items need to be taken from the table. The starting element could
---------------------- be at A[n. w] . If we trace the elements the optimal values can be derived. This
can mathematically be done as follows:
----------------------
a) If Ac[i, w] = A[i-1, w] : here  item i is not part of the solution,
----------------------
and b)with A[i-1, w] here item i is part of the solution.
---------------------- Analysis:
---------------------- This dynamic-0-1-kanpsack algorithm takes θ(nw) times. It can be broken
up as follows: θ(nw) times to fill the A-table, which has (n +1).(w +1) entries.
----------------------
Analyzing the algorithm shows that each requires θ(1) time to compute
---------------------- the sub-modules. 
---------------------- It requires O(n) time to trace the solution, Since, the tracing process starts
in row n of the table and it needs to be moved up 1 row at each step.
----------------------

---------------------- Check your Progress 1

---------------------- Fill in the blanks.


---------------------- 1. Divide and conquer uses _______ in its algorithms.
---------------------- 2. ______, ______ and _____ are the steps involved in dynamic
programming.
----------------------
3. In dynamic programming, _____ involves decomposing a problem
---------------------- into smaller and simpler sub-problems.
4. In case of dynamic programming, the optimal solution of the problem
----------------------
varies upon ____ and _______.
----------------------

144 Algorithms and Programming Concepts


Notes
Activity 1
----------------------
Apply Divide and Conquer and Dynamics for sorting 10 elements in ----------------------
descending order.
----------------------

----------------------
9.4 GREEDY ALGORITHMS
----------------------
Greedy algorithms are simple, easy and straightforward. They are
shortsighted in their approach which means that they take choices based on ----------------------
available information and by not fretting about the outcomes of these choices
that may be forthcoming. These are comfortable in development, easy to ----------------------
employ and greatly economical. Though not all problems can be solved using
----------------------
the greedy method they are useful to decipher optimization problems.
Characteristics and Features of Problems solved by Greedy Algorithms ----------------------

Algorithms manage with two sets in order to develop optimized solutions. ----------------------
One of these includes selected items while the other has rejected items. Greedy
Algorithm have four procedures: ----------------------

i. A procedure that inspects if the selected set of items give a solution ----------------------
ii. A procedure that inspects the viability of a set ----------------------
iii. A selection procedure that tells which of the candidates is the most
----------------------
conforming.
iv. An objective procedure, which inexplicitly provides the value of a ----------------------
solution.
----------------------
Organization of Greedy Algorithm
----------------------
●● At start, the answer set is empty.
●● At each stage ----------------------
 An element is added to the answer set by the selection procedure ----------------------
 If the set is not feasible, the elements are rejected.(and generally not ----------------------
considered at all)
 Else if the answer set is feasible then the elements are added. ----------------------

Definitions of Feasibility ----------------------


Feasibility is defined as possibility of existence. It is used to check if a ----------------------
process or task can be achieved.
----------------------
A feasible or viable set (of candidates) is confirming if it can provide not
only answers but also an optimal one to the problem. Particularly, the empty ----------------------
set is viable because a best possible or optimal answer can always exist. Few
algorithms are taken for study and analysis like the a) Huffman code b) fixed- ----------------------
length code and the trees are also reviewed. ----------------------

Special Problems and Algorithms 145


Notes a) Huffman code: It is a technique used for data compression. It checks
the occurrence of each character and its binary string in an optimal way.
----------------------
Problem: Consider a case which has data consisting of 100,000 characters.
---------------------- If this cluster of data is to be compressed, we can modify the characters in
---------------------- the data with following frequencies.
A B C D E F
----------------------
Frequency 45,000 13,000 12,000 16,000 9,000 5,000
---------------------- Consider the problem of designing a “binary character code” in which
each character is represented by a unique binary string.
----------------------
b) Fixed Length Code: In the application of fixed length code, 3 bits are
---------------------- required to represent six (6) characters.
---------------------- A B C D E F
Frequency 45,000 13,000 12,000 16,000 9,000 5,000
----------------------
Fixed Length code 000 001 010 011 100 101
---------------------- This method requires 3000,000 bits to code the entire data set.
----------------------  Here the total numbers of characters are: 45,000 + 13,000 + 12,000
+ 16,000 + 9,000 + 5,000 = 1000,000.
----------------------
 Each character is assigned 3-bit codeword => 3 * 1000,000 =
---------------------- 3000,000 bits.
---------------------- Conclusion:

---------------------- Fixed-length code requires 300,000 bits while variable code requires
224,000 bits for compression.
---------------------- => Saving of approximately 25%.
---------------------- c) Prefix Codes: In which no code word is a prefix of other codeword.
The reason prefix codes are desirable is that they simplify the complete
----------------------
process of encoding and decoding i.e. coding.
---------------------- d) Spanning Trees: A spanning tree of a graph is any tree that includes
every vertex in the graph. It can be also termed as a sub graph of a graph
----------------------
which is a tree and contains all the vertices. An edge of a spanning tree
---------------------- is called a branch; an edge in the graph that is not in the spanning tree is
called a chord. Spanning trees are used to find a simple, cheap and yet
---------------------- efficient way to connect a set of terminals (computers, cites, factories,
etc.).
----------------------
Spanning trees are useful for the following reasons:
----------------------
 They can construct a sparse sub graph that provides information
---------------------- about the original graph.

----------------------  Assist in designing efficient routing algorithms.


 Few complex problems (viz. Steiner tree problem and traveling
---------------------- salesman problem) can be solved approximately.

146 Algorithms and Programming Concepts


 They have wider applications in several areas such as network Notes
design, etc.
----------------------
Greedy Spanning Tree Algorithm
This spanning tree algorithm can: ----------------------
●● Examine the edges in graph in any arbitrary sequence. ----------------------
●● Undertake decisions if each edge will be included in the spanning tree.
----------------------
Each time an algorithm step is performed, one edge is inspected. In case
of a finite number of edges in the graph, the algorithm must stop after a finite ----------------------
number of steps. Hence, the time complexity of the algorithm is undoubtedly
----------------------
O(n), where n is the number of edges in the graph.
Greediness: Evidently, this algorithm has the characteristics that each edge is ----------------------
examined at most once. Such algorithms which can examine each entity at most
----------------------
once and decide its outcome once and for all during that examination are called
greedy algorithms. The most known advantage of the greedy approach is that ----------------------
we need not waste time reexamining entities.
----------------------
Let us consider the problem of finding a spanning tree with smallest
possible weight or the largest possible weight, called a minimum spanning tree ----------------------
and a maximum spanning tree respectively. It is simply that if a graph possesses
a spanning tree, it must have a minimum spanning tree and also a maximum ----------------------
spanning tree. These spanning trees can be done by performing the spanning ----------------------
tree algorithm with an appropriate ordering of the edges.
Maximum Spanning Tree Algorithm: The spanning tree algorithm should be ----------------------
implemented by checking the edges for non-increasing weight order i.e. first ----------------------
with the largest, and the last with the smallest one. In case of two or more edges
with identical weight, they need to be arbitrarily ordered. ----------------------
Minimum Spanning Tree Algorithm: The spanning tree algorithm should be ----------------------
implemented by checking the edges for non-decreasing weight order i.e. first
with the smallest, and the last with the largest one. In case of two or more ----------------------
edges with identical weight, they need to be arbitrarily ordered. To conclude,
the minimum spanning tree (MST) with a weighted graph (G) is a spanning tree ----------------------
of (G) where the sum of edges is the minimum weight. It means that a MST is ----------------------
a tree in a given undirected graph which is formed out of the subset of edges,
with the following two properties: ----------------------
i) It includes every vertex of the graph (i.e it spans the graph) ----------------------
ii) The total weight of all the edges is as minimum as possible.
----------------------
Problem:
----------------------
Identify a subset A of the edges of G where all the vertices are connected while
using only the edges A, while the sum of the lengths of the edges in A is the ----------------------
minimum possible.
----------------------

----------------------

Special Problems and Algorithms 147


Notes Solution:
Let G` = (V, A) be the partial graph formed by the vertices of G and the edges
----------------------
in A. [Note: A connected graph with n vertices must have at least n-1 edges and
---------------------- more that n-1 edges implies at least one cycle]. So n-1 is the minimum number of
edges in the A. Hence if G` is connected and A has more that n-1 edges, we can
---------------------- remove at least one of these edges without disconnecting (choose an edge that is
part of cycle). This will decrease the total length of edges in A.
----------------------
G` = (V, A) where A is a subset of E. Since connected graph of n nodes
---------------------- must have n-1 edges otherwise there exist at least one cycle. Hence if G` is
connected and A has more than n-1 edges it proves that it contains at least one
----------------------
cycle. Remove edge from A without disconnecting the G` (i.e., remove the edge
---------------------- that is part of the cycle). This will decrease the total length of the edges in A.
Therefore, the new solution is preferable to the old one.
----------------------
Hence an optimal solution would be where A is with n vertices and more
---------------------- edges. So, A must have n-1 edges and since G` is connected it must be a tree.
Here, G` is known as the Minimum Spanning Tree (MST).
----------------------

---------------------- 9.5 P AND NP COMPLETE PROBLEMS

---------------------- One of the intricate problems in theoretical computer science is that it


remains open, but continuous trials to solve it have provided adequate insight.
---------------------- P is indicatively a set of relatively easy problems, and NP is a set of largely hard
problems, so P = NP would mean that the apparently hard problems in fact have
----------------------
relatively easy solutions. However, it has more complicated details.
---------------------- What is NP
---------------------- NP is a set of all decision problems with answers in ‘yes’ or ‘no’. The
answers with ‘yes’ can be verified in polynomial time (O(n^k) in which n
---------------------- is the problem size, and k a constant) using a deterministic Turing machine.
Polynomial time in some cases is understood to be fast or quick.
----------------------
What is P
----------------------
P is a decision problems set that can be solved in polynomial time by a
---------------------- deterministic Turing machine and verified in polynomial time. Hence, P is a
subset of NP.
----------------------
What is NP-Complete
---------------------- A problem x in NP is similar to NP-Complete if and only if every other
problem in NP can be rapidly converted into x (in polynomial time). That is:
----------------------
1. x is in NP, and
----------------------
2. Every problem in NP is reducible to x
---------------------- So, if any one of the NP-Complete problems was to be answered quickly
then multiple NP problems can be solved instantly thereby making NP-Complete
----------------------
quite interesting.
---------------------- Also see: what’s “P=NP?”, and why is it such a famous question?

148 Algorithms and Programming Concepts


What is NP-Hard Notes
These are indeed hard problems amongst the hardest problems found in
----------------------
NP. It can be seen that NP-Complete problems are similar to NP-hard. Yet,
all NP-hard problems are not NP (or even a decision problem), even if ‘NP’ ----------------------
is a prefix to it. That is the NP in NP-hard does not mean ‘non-deterministic
polynomial time’. Yes it is highly confusing but the usage is entrenched and ----------------------
would not change.
----------------------
For example, in order to write an algorithm to find the largest one for an
unsorted list of numbers, the algorithm needs to look at all the numbers in the ----------------------
list as there would be no other way. But in case of simply keeping a record of
----------------------
the largest number it has encountered so far, it only needs to look at each entry
only once. Thus, the number of elements it handles is directly proportional ----------------------
to the algorithm’s execution time - which computer scientists identify as N.
Indeed, most algorithms are quite complicated, thereby less efficient, than the ----------------------
one for finding the largest number in a list; but many common algorithms have
----------------------
execution times proportional to N2, or N times the logarithm of N, or the like.
A mathematical expression that involves N’s and N2s and N’s raised ----------------------
to other powers is called a polynomial, and that’s what the “P” in “P = NP”
----------------------
stands for. P is the set of problems whose solution times are proportional to
polynomials involving N’s. Figure 9.1 shows the relationship between P, NP, ----------------------
NP Complete and NP hard problems.
----------------------

----------------------
NP
----------------------
NP- Complete
----------------------

----------------------

NP- Hard ----------------------

----------------------

P ----------------------
Fig. 9.1: Relationship between P, NP, NP-complete, NP Hard ----------------------
The relationship between P, NP, NP C can be represented mathematically as:
----------------------
●● P NP
----------------------
●● NPC NP
----------------------
●● P = NP
●● NPC = NP ----------------------

----------------------

----------------------

Special Problems and Algorithms 149


Notes
Check your Progress 2
----------------------

---------------------- Multiple Choice Single Response.


1. Which of the following procedures does Greedy Algorithm have?
----------------------
i. A procedure that inspects if the selected set of items give a
---------------------- solution
---------------------- ii. A procedure that inspects the viability of a set

---------------------- iii. A selection procedure that tells which of the candidates is the
most confirming.
---------------------- iv. An objective procedure, which inexplicitly provides the value
---------------------- of a solution.
v. All of the above
----------------------
2. A problem x that is in NP is also in NP-Complete if and only if?
----------------------
i. x is in NP
---------------------- ii. Every problem in NP is reducible to x
---------------------- iii. Both
---------------------- Fill in the blanks.
1. ______ is a technique used for compressing data.
----------------------
2. ______ is the set of all decision problems which can be solved in
---------------------- polynomial time by a deterministic Turing machine.
---------------------- 3. ______ problems are also NP-hard.

----------------------

---------------------- Activity 2

---------------------- Understand the Travelling Salesman Problem and solve the same using NP-
---------------------- Complete.

----------------------
Summary
----------------------
●● Divide and Conquer uses a top-down approach in its algorithms. The
---------------------- divide and conquer algorithms divide the question or problem into small
---------------------- sub-questions or sub-problems.
●● ynamic programming is an approach which uses divide-and-Conquer
D
---------------------- technique with a table.
---------------------- ●● Dynamic programming is more strong and refined design technique

---------------------- ●● Dynamic programming takes benefit of duplication and arranges to solve

150 Algorithms and Programming Concepts


each sub-problem only once, saving the solution (in table or in a globally Notes
accessible place) for future use.
●● he Greedy Algorithm has four procedures as - a procedure that inspects
T ----------------------
if the selected sets of items give a solution, a procedure that inspects the ----------------------
viability of a set, a selection procedure that tells which of the candidates is
the most conforming, an objective procedure, which inexplicitly provides ----------------------
the value of a solution.
----------------------
●● P-Hard are problems that are at least as hard as the hardest problems in
N
NP. This implies that NP-Complete problems are also NP-hard ----------------------

----------------------
Keywords
----------------------
●● Algorithm: Sequence of steps to perform the desired task
●● ivide and Conquer: It works by recursively breaking down a problem
D ----------------------
into two or more sub-problems of the same (or related) type ----------------------
●● ynamic Programming:  It is a method for solving complex problems
D
by breaking them down into simpler sub problems ----------------------
●● reedy Algorithm: An algorithm that follows the problem
G ----------------------
solving heuristic of making the locally optimal choice at each stage
----------------------
●● Sorting Algorithms: Algorithms used to sort lists
----------------------
Self-Assessment Questions ----------------------
1. Explain Divide and Conquer algorithm in detail with examples. ----------------------
2. Differentiate between P, NP-Complete, P and NP Hard.
----------------------
3. Write an algorithm for Insertion Sort using Divide and Conquer.
----------------------
4. What is Greedy Algorithm? Explain with an example.
----------------------
Answers to Check your Progress
----------------------
Check your Progress 1
----------------------
Fill in the blanks.
----------------------
1. Divide and conquer uses top – down approach in its algorithms.
3. Substructure, Table-structure and Bottom - up are the steps involved in ----------------------
dynamic programming. ----------------------
3. In dynamic programming, substructure involves decomposing a problem
into smaller and simpler sub-problems. ----------------------

4. In case of dynamic programming, the optimal solution of the problem ----------------------


varies upon how many sub- problems and _how many choices.
----------------------

----------------------

Special Problems and Algorithms 151


Notes Check your Progress 2
Multiple Choice Single Response.
----------------------
1. Which of the following procedures does Greedy Algorithm have?
----------------------
iv. All of the above
---------------------- 2. A problem x that is in NP is also in NP-Complete if and only if?
---------------------- iii. Both

---------------------- Fill in the blanks.


1. Huffman code is a technique used for compressing data.
----------------------
2. P is the set of all decision problems which can be solved in polynomial
---------------------- time by a deterministic Turing machine.
---------------------- 3. NP complete problems are also NP-hard.

----------------------
Suggested Reading
----------------------
1. Horowitz, Ellis, et al. Fundamentals of Computer Algorithms. Galgotia
---------------------- Publications.
---------------------- 2. Huang, Chung-Yang (Ric), Chao-Yue Lai and Kwang-Ting (Tim) Cheng.
“Fundamentals of algorithms”. Electronic Design Automation. Morgan
---------------------- Kaufmann Publishers.
----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

----------------------

152 Algorithms and Programming Concepts

You might also like