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

Dsa Notes

Chapter 3 introduces algorithms as a set of rules for solving problems, detailing their design, validation, analysis, and testing. It emphasizes the importance of pseudocode for expressing algorithms in a language-independent manner and outlines various control structures such as conditional statements and loops. The chapter also discusses the significance of time and space complexity in algorithm analysis, providing foundational knowledge for algorithm design and implementation.

Uploaded by

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

Dsa Notes

Chapter 3 introduces algorithms as a set of rules for solving problems, detailing their design, validation, analysis, and testing. It emphasizes the importance of pseudocode for expressing algorithms in a language-independent manner and outlines various control structures such as conditional statements and loops. The chapter also discusses the significance of time and space complexity in algorithm analysis, providing foundational knowledge for algorithm design and implementation.

Uploaded by

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

CHAPTER 3

CONLENRY COPY
Rer
e

APBORiTAM
1.1.ALGORITHMS: AN INTRODUCTION
An algorithm is a set of rules that must be followed when solving a specific
problem. The problem can be solved either by hand, usually, on a
or more
machine. Informally, we can also define an algorithm as a well-defined computa-
tional procedure which takes some value or set of values as input and generates
some value or set of value as output. The result of a given problem is the output
that we get after solving the problem. An algorithm is thus defined as a finite
the given input into the output
sequence ofcomputational steps that transforms to be correct, if for every input
for a given problem. An algorithm is considered terminated. Thus a correct
instance, it generates the correct output and gets
desired output
algorithm solves a given computational problem and gives the
value. On the contrary, we can say that the incorrect algorithm does not
terminate on the desired output for the input instance or might terminate for
some other output value other than the desired one.

An algorithm can be specified in any language. It can be specified in a natural


language like English or as a computer language, or even as a hardware design.
But the specifications must make sure that the resulting instructions are definite
and provide a detailed description ofthe computational procedure to be followed.

1.2. STUDY OF ALGORITHMS


Design of algorithms.
Algorithm validation
Analysis of algorithms
Algorithm testing
1.2.1. Design of Algorithms
Various designing techniques are available which yield good and useful
algorithm. Throughout this book we are dealing with the major methods of
algorithm design. These techniques are not only applicable to only computer
science but also to other areas such as operation research and electrical
engineering. Divide-and-conquer, incremental approach, dynamic
programming are such techniques. By studying all the techniques you can
formulate good algorithm.
1.2.2. Algorithm Validation
Algorithm validation checks the algorithm result for all legal set of input.
1
of Algorithm
2 00 The Design and Analysis
check the algorithm, whether it
the algorithm it is necessary to
After designing
result or not, for all possible
legal set of input.
the correct and desired (of any Language).
computes converted into the program
algorithm is not
In this phase the can be written. This
the method, a program
of
But after showing the validity verification. Here we check the
or program
known program proving statement
phase is as
set of input. It requires that each
program output
for all possible be correctly proved.
defined and all basic operations
should be precisely

1.2.3. Analysis of Algorithms available for solving a


are
situation may occur where many algorithms
A representedin
should be noted that data structure can be
particular problem. It of algorithms to implement
an operation
and there exists number
many ways
cases require, the comparison
of two algorithms
the said data structure. Such better one is
structure and later on the
on
operation on said data
to implement an
and space
chosen. The analysis of algorithm
focuses on time complexity
analysis,
time requirement for
the analysis of space
complexity. As compared to but wherever necessary, both the techniques
analgorithm is generally easier,is referred to as the storage required
in addition
are used. In this context space
needed by
to the space requiredto store the input data. The amount of memory
is referred to as space complexity. For an
program to run to completion
size of the input, thus, it is a
algorithm, time complexity depends upon the (program)
function of input size "n". The amount of time needed by algorithm
an
be noted that
to run to completion is referred as time complexity. It should
with the best
different time can arise for the same algorithm. Usually we deal
The minimum
case time, average case time, and worst case time for an algorithm.
amount of time that an algorithm requires for an input of size "n", is referred to
as best case time complexity, similarly average case time complexity is
the execution of an algorithm having typical input data of size "n". And lastly,
the maximum amount of time needed by an algorithm for an input of size, "n",
is referred to as worst case tinme compexity. Perhaps, there are many factors
which influence the time required by an algorithm. The analysis of an algorithm
is dealt in detail in subsequent chapters.
1.2.4. Algorithm Testing
This phase involves testing of a program. It consists oftwo phases: debugging
and performance measurement. Debugging is the process of finding and
correcting the cause at variance with the desired and observed behaviours
However, as E. Dijkstra has described, "debugging can only point to the presence
of errors, but not to their absence". Theperformance measurement or profiling
precisely describes the correct program execution for all possible data sets and
it takes time and space to compute results.
In this book great emphasis is laid on the designing and analysis of algorithm
and debugging and performance measurement are also dealt wherever required.

1.3. NOTATION FOR ALGORITHMS


We have expressed all the algorithms by Pseudocode. Here pseudocode means
that the algorithms that are presented are language and machine independent.
The prefix pseudo is used to give the information that the code is not meant to
be compiled and executed on a computer. It is easy to understand an algorithm
by using pseudocode. The pseudocode hides the implementation details and thus
Algorithm O03

one can solely focus onthe computational aspects of an algorithm.. Pseudocode


consists of keywords and English-like phrase which specify the flow of control.
In this section we will discuss the notations used for algorithms, that are
covered throughout the text of this book.
Algorithm Name
Bvery algorithm is identified by its name. The name of the algorithm should
be meaningful and followed by the word 'Algorithm', so as to represent the
algorithm name.
For example :
Algorithm Bubblesort
In the above statement, Bubblesort is the name of the algorithm.

Description
Every algorithm includes a briefdescription of the tasks that it performs. It
includes the name, type of a variable used and also the assumptions (if any).
For examnple:
Algorithm Bubblesort
This algorithm sorts the elements of given array 'A' in an ascending
(increasing) order. The array contains 'n'number of elements. The variable pc'
denotes the pass counter and lu' refers to the last unsorted elements. To count
the number of exchanges performed on any pass variable 'ex' is used.
Comment
The symbol ' indicates the comment. The comment specifies no action
and is taken only for clarity.
For example: Compareelement Ali] with Ali + 1]

Steps
Every algorithm contains steps to accomplish the particular task. Each step
begins with the phrase written in square bracket that sums up as briefly as
possible the principal content of that step.

For example:
Step 1 Initialisation]
setlu-n
set n n +1

Operators
For simplicity and clarity, we use mathematical language which includes
algebra and set theory. Basically, operators are the symbols which operates on
the operands. The operands are either constant values or consist of expressions.
The arrow "", performs the replacement operation, sometimes called
assignment or substitution.
For example : m-l
In the above statement, the value ofvariable 'm'is replaced by the current
value of variable ' . For clarity we have used a term 'set' to perform an
action.
Algorithms
and Analysis of
4 OO The Demign

setm- variable 'm' to the current


current value of the
the
which implies-aet
variable T.
value of the
also be possible,
Multiple assign ment
can

-ner

For exampe set m


:
and returns true
checks the given condition conditional
operators
The conditional upon the condition. The following
or false depending
in algorithms:
operators are used greater than equal
than >', less than equal to 'S,
Less than '<, greater
and not equal to "*.
to 2equal to =,
For example:
setm =n statement is wrong
n)statement is correct
if(m =

'and'and 'or'used which act as short circuiting.


are
The boolean operators we first
evaluate express1on
the given expression "e ande2,
Thusfrom results "true" then we evaluate "e," so as to determine the
e".If 'e, otherwise for the "false" value
of "e" the
result of the entire expression
entire expression cannot evaluate "true".
Similarly, in the expression
evaluates "false".
e or e" we evaluate "e," only
when "e,"
For example: correct
if(m =n and m Sp)>statement is
<p) statement is correct.
if(m = n or m

The exchange operator interchanges the value of two variables.


For example
setm n>Exchanging values
In the above statement, values of both the variables are interchanged
The above statement can also be written by using a new variable 'temp',
set temp-m,m+n, n-temp
or

set temp m
set m - n
set m temp
Variables
Variables can either be local or global. All variables are treated as local
variables if not explicitly specified as global.
The element of an array can be accessed by specifying the array name
followed by the index in the square brackets.
For example:
Alil, indicates the ith element of an array A. The length of an array is
specified by:
length A]
Scalar parameters are passed by value, that means
aslocal variables within "procedure" or
they are considered
"function". The array
are passed by reference, which means the same effect can beparameters
seen in an
array as in the "procedure" or "function".
Algorithm O05

1.4. STATEMENTS AND CONTROL STRUCTURES


Assignment Statement
The assignment statement can be of the following form
set p-m
In the above statement, the value of variable 'p' is replaced by the value
of variable 'm'.
Some more assignment statements are as follows
setp-p +1
settemp-Al
set e-0
set-1-1
set A [i +1)- temp
Multiple assignment statements can be indicated as follows;
setp-p +1,l-1-1
In the above statement, first the former expression is performed then
the latter one.
set p mEn
In the above statement, the value of variable 'p' and variable 'm' is
replaced by value of variable'n'.
Sequential Statements
In this case, the statements are executed sequentially. That means the
second statement is executed only after the execution of the first
statement. Some sequential statements are given
below:
setp-m ..)
set m n .()
set n n +1 ii)
set m P
.iv)
In the above ordered sequence of
statements, all the statements are
executed sequentially. That means one after another. Thus, the
statement in line (iii) is not executed before the statement given in line
(ii). The order of execution for the above statements is from line (i)
upto
line (iv).

Conditional Statements
These statements are executed
depending upon the condition specified.
It includes the 'if statements which takes the three form
aif
oif else
Dif-else-if
of AlgoPILli

Analysis
and
The Design
6 00

i f statements
if (expression) then

if-body

evaluates 'true' then #h.


if the value of expression
In the above statement, otherwise the control is transfer
are executed,
statements under if body'
after the end of if-body.
For example:
seta - 5

setb-6
if(a> 4) then
set a - a + 1

set b - a +1
After the execution of above statement, the final value of variable 'a' is6
and variable 'b' is 7.

if-else statements
if (expression) then

if-body

else
Algorithm OO7

else-body

In the above
statement, if the expression evaluates "true" then 'if-body'
statements are executed and control is transferred at the end of "
if-body"
ignoring the else-body). Otherwise, the 'else-body' statements are executed and
control is transferred at the end of
'else-body'.
For example:set a -5, b - 6 > assigning values
ifa> 6)

set a-a +1
set b -b +1

else

set a - a -1
setb-b-1

After the execution of above statement, the final value of variable 'a' is 4
and variable 'b' is 5, since expression evaluates false.

if-else-if statement
if (expression) then
Algorithms
and Analysis of
10 O0 The Design

case constant 3

default

In the above "case-statement" first the expression is evaluated then mate


is performed with the given "cases". If the match is found then the statements
1Ollowing that "case" are executed. If no match is obtained then the "deiau
statements" are executed and control is transferred at the end oT a

statement".
For example:set a 5 , b-6, ch-2
select case (ch)

case1
set a-a +1,b-b+1
return
Algorithm OO 11

case 2
set a a + 2, b b + 2
return
case 3
set a a + 3, b +b+3B
return
default
set a - a + 4, b -b+ 4

With the value of variable "ch" the


match is performed with respective
"cases. The match is obtained with "case2" in the
above mentioned "case
statement". The final values after
b = 8. executing the above statement are : a 7, =

The "case statement" also be nested. The of the


can
general form nesting of
"case-statement"is given below:
select case (expression)

caseconstant 1:

select case (expression)

caseconstant 1:

case constant 2:

evaluated then match


then the statements

ined thenthe "default


at the end of "case
ms

14 CO The
Design and Analysisof Algorith
= = ] statements

count +1
set count-
Forexample:
setq-5
2, > (ns 10)
Repeat forcount- 1,
--n

incrementing qby 5.
setq-+5
of above statements the final valito
variable "¢i
se

After the completion


consider the above "Repeat ep, then
statement" ster
we have:
55.If we
count-1 step 1
10

count2 step 2
15

count3 step 3
20
count4 step 4
25

count 5 step 5
30
count 6 step 6
g-35
count7 step 7
40

count8 step8
45

count 9 step 9
-50
count 10 step 10
55
Now, if"count" increments then its value exceeds the last "sequential value"
and loop terminates.

Repeat while (condition)


The above iterative statement is used only when a set of instructions (steps)
are required to be repeated number of times until the given condition evaluates
false. Initially the condition is checked, if it evaluates "true" only then the
statements in the loop are executed and the whole process continues until the
given condition evaluates "false".
Some interative statements considering "condition" are given below.
Repeat while (a 2 b)
Repeat while (a < b)
Repeat while (a > b andb>c)
Repeat while ( a # NIL and b > c)
Repeat while (true)
Algorithm 00 15
-

For example:set a 5,b - 10


Repeat while (a < b)
set a - a +1
When the above statement is executed then
the final value of variable 'a' is
10.
Ifwe consider step by step method then we have
Initially,
a5
b10
a <b, true
step 1
a-6
a <b, true
step 2
a-7
a <b, true
step 3
a8
a <b, true
step 4
a-9
a <b, true
step 5
a-10
a <b, false step 6, loop terminates
It should also be noted that if
initially the condition evaluates "false" then
loop is terminated by doing nothing.
Repeat for count sequence while (condition)
This form of "Repeat iterative
statement is the combination of both the
above "Repeat-statements" discussed earlier. It is used when it
is required that
a set of instructions
(steps) are repeated for a sequence whose values are taken
successively by count until a given condition evaluates
Some iterative statements 'false.
of this type are given below
Repeat for count- 1, 2, 3 n while (a <b)>ns50
- -

Repeat for count - 1, 2, 3 10 while (a > b).


---

For example :set a -1,b-5, count-1


Repeat for count- 1, 2,3 --- 10 while (a < b).
set a -a +1
After executing the above
statements, the final value of variable 'a' is 5.
Considering step by step method we have:
count-1
a<b, true
a2

count-2
a <b, true
a3
Algorithms
Analysis of
OO The Design and
16

count3

a <b, true

a4

count4

a <b, true
a5
"false" thus the loop terminates.
condition becomes
Now, the
Unconditional Statements transter of the control. The
statements cause
an unconditional
These :
statements are of following types
unconditional

go to
Oexit loop
O exit. unconditional transfer ofthe controlto
causes an
The "go to statement" the control to transfer to
"go to step 3"
causes
the specified step.Thus
called steps).
in between calling and
step 3 (omitting all the steps
causes
unconditional transfer of the control
T h e "exit loop statement" it immediate exit
causes an
That means
outside the loop (or brackets).
The "exit loop
works
statement"only for one
from the loop (or brackets). one loop is nested within
are there and
level. Thus, if nested loops
statement of inner loop causes
of "exit-loop"
another, then the execution
to transfer the control to
the outer loop.
is written as the last step, which
The "exit statement" statement
terminates the whole algorithm.
It should be noted that the above
mentioned unconditional statements leads to unstructured algorithm,
so these statements
should be avoided. The whole text book takes the
are used only
advantage of structured algorithms but these statements
when the alternatives are awkward.

For example:
Now, consider an example that sorts a given list of items.
1.5. ALGORITHM BUBBLESORT
This algorithm sorts the element of an array 'A' (having n elements) in the
and
(increasing)order. The counter is denoted by variable P
ascending
the variable 'E' is used
pass
to count the number of exchanges performed on any
pass. The last unsorted element is referred by variable ".

Ste Initialization
set 1- n,p 1
Step2 10op
Repeat through step 4while (p Sn 1)
set E0 Initializing exchange variable,
Step 3 comparison, loop.
Repeat forie1,1,...l-1
Algorithm O0 17

iftAti]> Aí+ 1) thenm


set A[i]Ai
set EE+1 +11Exchanging values.
Step 4 Finish, or reduce the size.
if(E 0) then
exit
else
setI--1.
Explanation
I n the
smallest key.
algorithm, pass refers to the search for the
At
element with next
a time each pass
Thus, for
places one element in its
proper position.
I n pass performing the above sort 'n 1 '
passes are
and the
adjacent elements are compared such required.
1, the
as A[1] and Al2,
elements are arranged in
A[1]. After that A[1] and proper order like A[2], A (1] (if A(2]<
the greatest element is A[3] are compared. The process continues until
the largest element. In placed at the last position. Thus, Aln] contains
this pass (n 1)
I n pass 2, the second comparisons are required.
-

(n 2) comparisons.
largest element is placed at An 1]
by performing
-

After n 1 passes we
get the
-

final
sorted list as:
A SA2SA3S...A-1SA
The whole list of 'n' elements of an
array 'A"'is sorted after (n 1) passes. -

Example 1.1. Following lists of elements are given, and we have to sort the
given list in an ascending order using Bubblesort.
45, 60, 40,95, 75,35, 25,65.
The whole process is illustrated below:
Solution: The Bubble sort process is given
show the comparison.
below. The encircled elements
Initially,
G5 6
45 60 40 95 75 35 25 65
65
Pass 1
Compare a and aj
a4 5
45 40 95 75 35 25 65
as a a S0 no change in the resultant list.

Compare a, and a2
a1 a4 5 6 O7
45 95 75 35 25 65
as a a g so the resultant list is.
of Algorithms
and Analysis
18 O0 The Design
a6
a3 4 65
a2 35 25 1.6. SUBALGORITHM
95 75
45 40 60 The main algorithm
Comparea, and a3 subalogorithm is a co
2 a4
65
designed to performn
75 35 25 control of the main
45
45 40 algorithm can use it
resultant list.
s0 no change in the The subalgorithm an
as
a2 ag,
program and main pr
Compare ag and a4
a4 performs computatio
a1
25 65 routine. It should be
45 40 60 95 (75) 35
terminating the algo
So the resultant list is
nating the subalgoritt
as
ag a4,
5
a1 a3 4
95 35 25 65 [Procedure/F=
45 40
40 60 75 The subalgorit
Compare a, and ag Function subal
a6 of a Procedure
a2 value of the fur-
25 65
45 40 60 75 95 1.6.1. FUNCTION SUE
resultant list is.
asa>ag, So the Function subalgo-
a1 a2 ag 4 a5 tine. It usually has tE
60 75 35 95 25 65
45 40
Compare a, and Function Subal
a1 4 a6 The returning of
statement 'return (v
45 40 60 75 35 95 25 65
as aga6, So the resultant list is Example 1.2. The fo
rameters and return
a2 4 a5 a6
45 a Solution
45 40 60 75 35 25 95 65.
Compare ag and a Function Add (=
ag a4 a5 a6 The abovefunc
45 40 60 75 35 25 95 65 2 All variables are
variable w' which
as aga7, So the resultant list is
Step 1 com
do 1 d2 4 a5 a6 a7
45 40 60 75 35 25 65 95. set
A t the end of first pass, the largest element 95 has moved to the last posi Step 2retu
tion i.e., dq retu
The main algori
4
45 40 60 775 35 s25 a6 65 95
part of an expressio
A t the end of second pass, the second largest element 75 has moved to the For example: A
set
second last position ie., ag
It should be not
a7 the parameters of e
40 45 60 35 25 65 75 95 is executed then th
The process continues till
pass 7 where the seventh largest element has to ' . After the exe
moved to the position a. Thus, we get the sorted list after n 1 passes. -
variable'w' is retur
2 4 5 a6 statement. Finally
25 35 40 45 60 65 75 95
Algorithm O0 19

1.6. SUBALGORITHMM

ithm
The main algoriti can call zero, one or more than one subalgorithm. A
alogorithm is a complete and independently defined algorithm module. It is
sub
form
desigmed to perfor some computational tasks, whereever necessary under the
control of the main algorithm. It is defined independently so that different
algorithm canuse it or even the same algorithm can invoke it number of times.
The subalgorithm and main algorithm have the same relationship as the sub-
program and main program have in a programming language. A sub algorithm
performs computatIon on zero or more parameters provided by the calling
routine. It should be noted that the main algorithm uses 'exit statement' for
terminating the algorithm and subalgorithm uses 'return' statement for termi-
nating the subalgorithm. Usually subalgorithm takes the following form:
[Procedure/function] subalgorithm name (par 1, par 2 par n)
The subalgorithm can be of two types either 'procedure subalgorithm or
Function subalgorithm'. The statement return' marks the dynamic end
of a Procedure' or a Function' and in the latter case it also supplies the
value of the funetion.
1.6.1. FUNCTION SUBALGORITHMM
Function subalgorithm explicitly returns the single value to the calling rou-

tine. It usually has the following heading:

Function Subalgorithm (par 1, par 2, par n)


the
The returning of value and transfer of the control is accomplished by
statement 'return (value).

Example 1.2.The following function subalgorithm adds the values of the pa-
rameters and returns the added result to the calling routine.

Solution:
Funetion Add ( ,, 2
T y and
The above function subalgorithm adds the values of variables
stored in
All variables areassumed tobeof type real. The addedresult is
variable w which is returned to thecalling routine.
Step 1 computing, sum
setwx+y+
Step 2 return, computing result
return (uw)
The main algorithm involves the function subalgorithm 'Add' as an implicit
part of an expression.
For example: An algorithm contains following statement.
set sum - Add (i,j, k)
It should be noted that there should be one to one correspondence between

parameters of called and calling routines. Thus when the above statement
the
1s executed then the value of i is copied to ' ' , f is copied to y and '*' is copied
to 2. After the execution of subalgorithnm 'Add' the computing result value of
kY in the
variable 'w' is returned to calling subalgorithm and replaces Add (i,j,
Statement. Finally, the variable 'sum'holds the value of the variable
and Analysis ofAlgorithms
The Design
20 0 0
1.6.2. PROCEDURE SUBALGORITHM

no
icitly returned to the
value is explicit calling routine.
subalgorithm e than one value t
more:

In
Procedure wants to
return
the cal:
used when one
implicitly through parameters
hurned implicitly parameters. It will us
Infact,it is
routine. All values
are
returned

form
ually
have the
heading ofthe par n)
Subalgorithm (par 1, par 2,-
Procedure
for Procedure Subalgorithm
example
Consider following
following Procedure
Subalgorithm swaps the
the values
valuee.
of
Krample 1.3. The the
return implicity.
parameters,
all values

Function Swapping (, y variable


The above proceduresubalgorithm swaps the values of
to be real.
and
variables are assumed
y.ll
parameters
Step 1 swapping, values of
set x
Step 2 returning, at point of call
return.
Inabove procedure subalogorithm, novalue is explicitly returned,but
instead we get the desired computed result.
It should be noted that any Punction Subalgorithm can be easily trans
lated in to the Procedure Subalgorithm. This is done just by adding a new
parameter which is usedto return the computed value to the calling routine
Erample 1.4. In this Function Subalgorithm 'Add' can be translated into an
equivalent Procedure Subalgorithm.

Procedure Add,y, 2, u
Theabove Procedure Subalgorithm adds the value to the variable
and 2. Variable u holds the computed result of the addition. All variables
are assumedto be of type real.
Step 1 computing, sun
Setw +y+2
Step 2 returning, at point of call
return
1.7. ANALYSING ALGORITHMS
Analysis of algorithm depends
communication bandwidth, or upon various factors such as
the computer hardware. But the most often memory
computational
task. As
time that an
algorithm requires for completing theusea
1

algorithms are machine and


important, durable, and original language independent, these are thegve
our
design and implementation for parts of Computer Science. Thus, we will ou
the ao
model all
instructions are executed RAM model of computation. In the *AM
concurrent operations. In sequentially, one after another, w no
subtraction, assignments etc, performing
model takes simple operations like adaid
loops are not single one step. A
call to a
one step. step operation. Instead,, each subrouan
By counting the
measured. number of steps the memory access takes kactlyis
runtime of an
agor
Algorithm O0 21
-

e Analysis of an algorithm focuses on time and space complexity. The


complexity refers to the amount of memory required by an algorithm to
completion. Time complexity is a function of input size, 'n'. It is referred
he amount of time required by an algorithm to run to completion. Perhaps,
e n t time can arise for the same algorithm. We usually refer best case time
complexity, average case time complexity, and worst case time complexity. Fig.
1.1 shows the possible time complexity of an algorithm.
No. of steps
Worst case

Average case

Best case

N
2 3
Fig. 1.1.Time-complexity
The worst case time complexity is the function defined
by the maximum
amount f time needed by an algorithm for an input of size, 'n'. Thus, it is
the function defined by the maximum number of taken on
steps any
instance of size 'n'.
The average case time complexity is the execution
of an algorithm having
typical input data of size 'n'. Thus, it is the function defined by the average
number of steps taken on any instance of size 'n'.
The best case time complexity is the minimum amount
of time
that aan
algorithm requires for an input of size 'n'. Thus, it is the function defined
by the minimum number of steps taken on any instance of size 'n'.
t should be noted that all these time
function time vs size.
complexities define a numerical

1.7.1. Analysis of Insertion Sort


The insertion sort is based on the
"Bridge-player" method, in which players sort
their hands by selecting one card at a time and
place
it into its
position. The insertion of record in this method is done in such a wayáppropriate
that when
ever a new record
'R is inserted, its preceeding records R, R2, R,1 have
already been sorted.
Assume that:
1<jsn, the new record "R;" is inserted in the table, having the key "K;",
such that the itspreceding record R,, R2, . R have already been sorted,
where their respective keys follow the order
as:
K- 2K-2 2Kg 2K22 1.
and
Analysis ofAIgUT.

Design
The
22 O O
A, n)
the elements of an
Insertionsort

P r o c e d u r e S u b a l g o r i t h m
sorts
n' 'number of element
array 'A:
The
Procedure

The array
consists of n
variable,
The variable
order. a temporary
ascending and ?' is
for index [A]
fis used
uptil length
for i 2 , 3, 5,
Step 1
Loop, 4, -n

Repeat step 1,2


set t A [ ,
p -i-1 new value, pointer is
temporary
variable isset to adjusted
comparison
loop, <Alp)
Step 2 > 0 and i
Repeat while (p
pt-p-1
setAp +1] 2 Alpl
loop
End of step
place
I n s e r t i n g element
inaPpropriate
Step 3
et A (p + 1lt
ofstep 1 loop
End
Step 4 Finished
return that a r e executod
statements
individual
times of
the running if a s t a t e m e n t is executed in
By summing time algorithm. Thus,
w e get the
running of an times then the
total running time of the
number of
executed 'n'
steps and is
statement is Cin' algorithm according to cost
insertion sort'
our
we are
summarising
Now,
and time' taken
as:
Cost Times
Insertionsort (A, n)
-2, 3 n C n
7 for i
. . .

1. Repeat through step C2 n-1

2. sett-A] n - 1
Ca
3. setp -i-1
>0 and t <Ap))
CA i=2
4. Repeat while (p

1]-A(p]
C6 4-
5 set Alp +

setp-p -1
C6
6.
n-1

1)-t C
7. set Alp +
rall
results in over
'cost' for comments is '0' which the tota
It should be noted that the executable s t a t e m e n t s . Thus,

z e r o running
time as comments a r e not
as
Tln) c a n be expressed
running time of an algorithm - 1)

Tn) = Cn+C,(n -1)+C,(n -1)+C E; +C, Ë(4


-

1) +C, E(G -1)+C


i = 2
Algorithm OO 23
Best Case
Insertion sort in best case is when the
array is already sorted,
1 , and there should be such that
no comparisons (for i =
1, 2, n). Thus,
.

Tn) =C,n+C(n 1)+Cg(n 1)+C, (n - 1)+ C, (n 1


-
-

The above (C+C2+Cg+ C+ C,)n -(C2 C+C+C). +

' and 'b' are


running time is of the form a +b, which is linear order, in which
constants that
Worst Case
are
depended upon the 'cost' of the statements.
Insertion sort in worst case is
order (reverse when the is sorted but in
order). In this case, one must array decreasing
each
elements in the entire sorted compare each element A[j with
,=ifor i=2, 3, ...n. subarray A[1], A[2], . A[i -1), and thus,
From all we have,

l = 2
nn+1)-1
2
and

2i-1) =n(n -1)


i=2 2

Thus, by substituting above values


lgorithm in worst case as we get the total running time of an

Tt)=Cn+G(i -1)+C,n-i)+c -1
+C(n-1)
2 2

It can be that the worst


seen
+(Cg +Cg +C +C)
case time can be expressed as
which is a
quadratic order. an + bn +C,
Average Case
In an average case, at
any time we can take 'n'
the insertion sort random numbers and
algorithm. It can be easily
observed apply
that average case is often
as bad as the worst
case, as on an half the elements in
Ai-1] are less than A[), and half average elements are greater. Thus A[1], A[2] .
ofthe subarray A[). A2)...A|-1] on an the we check half
the running time of average. Thus t=i/2. If we calculate
turns out to be a
algorithm in average case, then
like in the worst case, it
quadratic function of the input size.
Example 1.5. Suppose an array contains 8
elements as follows:
70, 30, 40, 10, 80, 20,
and we have to sort it in an 60, 50.
The whole process is ascending order using insertion sort algorithm.
illustrated below.
Algorithms
and Analysis of
24 O0 The Design
indicate the element in ean
h
encircled elements
Solution The :
a r r o w s show
the proper place for pass of th
inserting at
algorithm and pointing
Step 1
element
pass 1

40 10 80 20 60 50
30
Step 2
pass 2
2
40 10 80 20 60 50
70
Step 3
pass 3
ds 6

30 70 10 80 20 60 50
Step 4
pass 4
6

30 40 70 1 80 20 60 50
Step 5
pass 5
o as 6

10 30 40 70 20 60 50
Step 6
pass 6

10 30 40 70 80 20 60 50
Step 7
pass 7

10 20 30 40 70 80 50
Step 8
pass 8

10 20 30 40 60 70 80
Sorted array
a4 ag
10 20 30 40 50 60 70 80
1.8. DESIGNING ALGORITHM
Various designing techniques are available for algorithms. Here we will disc
two designing approach
of algorithm:
Algorithm O0 25

Incremental approach.
Divide and conquer
Approach.
1.8.1. Incremental Approach
In this
approach whenever element is inaerted into its appropriate
a new
place, index is increased. Previously discussed
on this insertionsort algorithm is baased
approach; which we have sorted
in
of new
element A[] into appropriate placesubarrayA[1],
yields sorted
A[2] Ali -1), insertion
subarray A[1), A|2)
Ali 11 A[i].
Here,
based
we are
presenting another sorting method 'selection sort' which is
on this
approach. In selection sort
are selected in order method, all the successive elements
and placed into their
begins by finding the least element which isappropriate positions. Thea method
After finding the least accomplished by making search.
element, it is then interchanged with the first element.
Next, second smallest element is
whole interchanged with the second element. The
process continues until the whole list is sorted.
Procedure Selectionsort (A, n)
The above Procedure
Subalgorithm sorts given element of an array
A' of n number of elements in an ascendingthe order. The
that is located in particular pass is denoted by variable 'S'.smallest element
The variable p
denotes the index of a pass and position of the first element which is to be
examined during a particular pass.
Step 1 loop, repeated (n-1) times
Repeat through step 3for p1,2,3
set S-p
Step 2 element with smallest value is obtained in every pass
Repeat step 2for i -p+1,p+2n
if(A[S]> A[il) then
set S-i
End of step 2 loop
Step 3 Exchanging the values
if(S#p) then
set Alp]+ A[S]
End of step 1 loop
Step 4 Finish
return
Analysis
It should be noted that while calculating the running time of selection sort
algorithm, the exchangingstep breaksdown into three individual step as
sett-Alp]
setAlp]-Als]
set A[s) - t
Now, collecting all we have the following running time Tn):
26 00 The Design and Analysis of Algorithms

Selectionsort ( A, n) Cost
Times
1. Repeat through step for p- 1, 2,... n -

1
C Step 2
2. set S - PP Ca - 1
pass 2
S 5
3.
Repeat step 3 foriep+1.p+ 2 . C3
P 1

4. if Ala)> A[j then CA step 3


Pass 3:
p 1
S=5
5. set S-i
C6 -1
p 2

6. if (S+ p ) then Step 4


Co E-1
p 2 S
Pass 4:
5

7. set t-Ap]
C
Step 5
8. setA[p]-A[s] Cs Pass 5
-1
p 2
S=7

9. set A[s] - t
C9 2-1
P=2
Step 6
Tn)
G,n)+C;(n -1)+c P=1
+C +C, -1)
pass 6
S 6
p=1 p 2

Ca ,-1)+c, ,-1)+C ,
P=l -1)+C,, -1)
P-2 P=2 Step 7
P 2
Example 1.6. Let us suppose an array consists of 8 pass 7
elements as S= 7
70, 30, 40, 10, 80, 20, 60, 50.
and we have to sort the
elements in an
method. The whole process is ascending order using selectionsort
illustrated below.
Solution : The encircled elements
denote the smallest key selected Sorted
pass, which is then in a particular
exchanged.
Step 1
pass 1
S 3
1.8.2.
as Mc
70 30 40 80 20
partic
60 50 algorit
In thie
Algorithm O0 27
Step 2
pass 2:
S=5
a2
10 30 40 70 80 20 60 50
step 3
Pass 3:
S 5
o
as
10 20
40 70 80 60 50
Step 4
Pass 4:
S 5
3 as
10 20 30 70 80 60 50
Step 5
Pass 5
S=7
2 as
10 20 30 40 80 70 60 50
Step 6
pass 6
S=6
a6
10 20 30 40 50 70 60 80
Step 7
pass7
S= 7
6
10 20 30 40 50 60 80
Sorted array

d2 5 d6
10 20 30 40 50 60 70 80
1.8.2. Divide and Conquer Approach
Most of the algorithms are recursive in nature. That means, for solving a
particular problem they call themselves repeatedly one or more times. Al these
agorithms follow the divide and conquer approach to accomplish the given task.
n this approach, whole problem is divided into several subproblems. These
Design
and Analysis of Algorithms
28 O0 The
oblem
problem but small
Bmaller insize. All
to the original
subproblems
are
similar
recursively. The solutions obtained om
from
these
are then solved
create a solution to the orioin the.
the
subproblems combined to
are then
subproblems
At blems
each levelof the recursion the divide and conquer approach followsthree

steps
S t e p1 : Divide
In this step whole problem is divided into several subproblems

Step 2: Conquer
The subproblems are conquered by solving them recursivelu
they are small enough to be solved, otherwise step 1 is executed
Step 3 : Combine
Finally, the solutions obtained by the subproblems are combined t
create solution to the original problem.
Here, we are presenting a simple problem that can be solved by the divide
and conquer technique. The problem is to sort a given list of item using merge
sort. For solving the given problem it includes these steps as

Step1: Divide
The whole list is divided into two sublists of n/2 elements each for
sorting.
Step 2: Conquer
Sort the sublist recursively using mergesort.
Step 3: Combine
Now Merge the two sorted sublists to generate the sorted answer.
For accomplishing the whole task we are using two Procedu.res "Mergesort
and Merge'. In this section we are presenting the algorithm for procedure
"Mergesort' which takes the advantage of Procedure 'Merge'. Procedure 'Merge'
is used for combing the sublists.

Procedure Mergesort (A, start, finish)


The above Procedure Subalgorithm recursively sorts given list of elements
between position "start' and "finish" (inclusive). Array A' contains 'n' number
ofelements. Variables length' and 'mid' refer to the number of elements in the
current sublist and position òf the middle element of the sublist, respectively.

Step Computation, size of current sublist


setlengthfinish-start+1
Step 2 Conditionchecking, if length is one
if(length 1)then
return
Step3 caleulating, middle point
set mid-sort +length21-1
Step 4 solving I sublist,recursively
call Mergesort (A, start, mid)
Algorithm O0 29

recurslvel
Solving I sublist,
mid + 1, finish
Mergesort (A,
call
two sublists, sorted
Merging
mid +1, finish)
Shep6
call Merge (A, start,
Finish

return

Merge (A,
first, second, third)
dure the two lists and produces a
proceae Drocedure subalgorithm merges
T h ea b o v e p r o c e d u r e
new
is used for holding sorted values.
The Temporary
array temp'
sortedl i s t .

initialisation

Step 1 S - second
setn 0 , f - first,
element
Comparison, giving smallest
Step ifAIISASD then
+1
setn - n
set temp In)-AIN
setf-f+1
else
setn - n +1
set temp n]-A[S]
setS-S+1
elements
Stap3 Copying remaining
if (f2 second) then
loop
Repeat while (S S third)
set n - n +1
set temp [n]-A[S}
set S-S+1
End loop
else
loop
Repeat while (f< second)
setn -n +1
Bet temp Inj -A[S)
setf-f+ l
End of loop
Step 4 Copying, element to original array
loop
Repeat forf-1,2, - n
Alfirst-1+f1-temp f1
End of loop
Step 5finish
return

You might also like